Linq: GroupBy, Sum and Count

后端 未结 3 2204
庸人自扰
庸人自扰 2020-11-22 15:45

I have a collection of products

public class Product {

   public Product() { }

   public string ProductCode {get; set;}
   public decimal Price {get; set;          


        
3条回答
  •  不要未来只要你来
    2020-11-22 16:07

    I don't understand where the first "result with sample data" is coming from, but the problem in the console app is that you're using SelectMany to look at each item in each group.

    I think you just want:

    List result = Lines
        .GroupBy(l => l.ProductCode)
        .Select(cl => new ResultLine
                {
                    ProductName = cl.First().Name,
                    Quantity = cl.Count().ToString(),
                    Price = cl.Sum(c => c.Price).ToString(),
                }).ToList();
    

    The use of First() here to get the product name assumes that every product with the same product code has the same product name. As noted in comments, you could group by product name as well as product code, which will give the same results if the name is always the same for any given code, but apparently generates better SQL in EF.

    I'd also suggest that you should change the Quantity and Price properties to be int and decimal types respectively - why use a string property for data which is clearly not textual?

提交回复
热议问题