0

Is it possible to specify only certain fields from a table when using Linq and possible include a RecordSet count?

Here is my existing query within the HomeController.cs

var contentModel = from m in db.Contents where (m.NavigationId == 1) && (m.Active == true) orderby m.Position select m;
return View(contentModel);

I would like to include Id, Title, Image and Summary, set the maximum number of records within the recordset to 12 and return an extra value which is the actual number of records/12 and pass to the View.

Any help would be much appreciated :-)

iggyweb
  • 2,373
  • 12
  • 47
  • 77
  • What do you mean by extra value? You can use `.Take(12)` to retrieve only 12 records –  May 03 '14 at 09:56
  • I've modified my query as var contentModel = (from m in db.Contents where (m.NavigationId == 1) && (m.Active == true) orderby m.Position select m).Take(12) which works but I need to pass another value to the view which is the actual number of records, lets say 2 to be divided by the maximum number 12 so should return 6. – iggyweb May 03 '14 at 10:01
  • Also I wish to only select Id, Title, Image and Summary not all fields. – iggyweb May 03 '14 at 10:02

3 Answers3

0

Instead of select m you can use

 select new { m.Name, m.Id, m.SomeOtherProperty }

Or if you want to pass that result to another method as a return value

 select new ContentsViewModel()
 {
     Name = m.Name,
     Id = m.Id,
     SomeOtherProperty = m.SomeOtherProperty
 };

Where ContentViewModel is a second POCO class that looks like:

 public class ContentViewModel()
 {
      // Must have a public constructor with 0 parameters
      public ContentViewModel(){}

      public string Name{get;set;}
      public int Id{get;set;}
      public Something SomeOtherProperty{get;set;}
 }

This cannot be an Entity Framework class due to limitations in Entity Framework described here.

For the count issue, see: https://stackoverflow.com/a/7771298/736079

Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • For some reason, despite Visual Studio 2013 not having issue with var contentModel = (from m in db.Contents where (m.NavigationId == 1) && (m.Active == true) orderby m.Position select new { m.Id, m.Image, m.Title, m.Sub, m.Summary }).Take(12); it is now returning a Null recordset. – iggyweb May 03 '14 at 10:41
  • The query looks ok from a glance... Always hard to visually see what's wrong without the actual context and data. Btw, when inlining source code in a comment, please surround it with backticks ``, that way they get highlighted: `code`. – jessehouwing May 03 '14 at 16:34
  • I don't understand it either, if Visual Studio 2013 can't find issue I have no chance of figuring it out. I'm having same issue with AspNetUsers table, all I want is number of total records minus one user. Just a number, no extra fields as that's not efficient sql, not a fan of Linq, bring Back T-SQL and Classic ASP LOL ;-) – iggyweb May 03 '14 at 18:24
0

You need something like this:

   Var contentModel = db.Contents.Where(m=>m.NavigationId==1)
                   .Where(m=m.Active=true).Take(12).Orderby(m=>m.Position).Select(m=> new {m.Id, m.Summary, m.Title,m.Image});

But I am not sure to understand what you mean by number of record by 12. As you already set the number of record by 12.

Your result will be a IEnumerable to access field you need to iterate.

Regarding the number that you want to pass, you can use ViewBag object: In your countroller:

   Int number = contentModel.ToList().Coun()/ 2;
   ViewBag.myNum=number;

In your view you will use the ViewBag.myNum casted to int. It is an option that you can also try.

Hope it will help.

  • Thank you, the code I need was int number = 12 / contentModel.ToList().Count(); and the value was 6 as expected and did pass to the controlled, however
    actually renders as article class="span @ViewBag.rs"> and not article class="span6">
    – iggyweb May 03 '14 at 10:27
  • If I use
    it renders as article class="span 6"> which obviously doesn't target the span6 css.
    – iggyweb May 03 '14 at 10:29
  • Try `@HTML.Raw(ViewBag.rs)`, hope it will help –  May 03 '14 at 10:32
  • You can also inclue the `span` in the `ViewBag` in the controller. `ViewBag.rs="span" + number.ToSttring();` and then in the View `
    `. It is also a way to do it
    –  May 03 '14 at 10:33
0
var contentModel = from m in db.Contents where (m.NavigationId == 1) && (m.Active == true) orderby m.Position 
select m.Id, m.Title,m.Image,m.Summary;
var top = contentModel.Take(12);
int max = top.Count();
int Count = contentModel.Count();
var avr = Count/max;
return View(avr ); 

or return View(top); etc

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31