I'm working on a new project using .NET MVC4 and Entity Framework. I have a products table and i'm trying to select products with 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 = from p in productList
group p by p.id into grp
select new
{
Index = grp.Key,
ProductList = grp.ToList<Products>(),
ItemCount = grp.Count(),
Total = grp.Sum(w => w.price)
};
And then i wanted to apply result to a List of Cart. But i failed at some point. Please can you help me to do that.
You need to specify the type returned by your select
var result = from p in productList
group p by p.id into grp
select new Cart //<-- here
{
Index = grp.Key,
ProductList = grp.ToList<Products>(),
ItemCount = grp.Count(),
Total = grp.Sum(w => w.price)
};
now result
is of type IEnumerable<Cart>
. You could add an extra ToList()
to make it List<Cart>
:
List<Cart> lst = result.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.