c# - Can't fill my model with a grouped list of a table using LINQ -


i'm working on new project using .net mvc4 , entity framework. have products table , i'm trying select products grouping.

my model:

public class cart {     public long index { get; set; }     public list<products> productlist = new list<products>();     public int itemcount { get; set; }     public decimal total { get; set; } } 

my query:

var result = p in productlist              group p p.id grp              select new              {                  index = grp.key,                  productlist = grp.tolist<products>(),                  itemcount = grp.count(),                  total = grp.sum(w => w.price)              }; 

and wanted apply result list of cart. failed @ point. please can me that.

you need specify type returned select

var result = p in productlist              group p p.id grp              select new cart  //<-- here              {                  index = grp.key,                  productlist = grp.tolist<products>(),                  itemcount = grp.count(),                  total = grp.sum(w => w.price)              }; 

now result of type ienumerable<cart>. add tolist() make list<cart>:

list<cart> lst = result.tolist(); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -