I have a collection of products
public class Product {
public Product() { }
public string ProductCode {get; set;}
public decimal Price {get; set;
sometimes you need to select some fields by FirstOrDefault() or singleOrDefault() you can use the below query:
List result = Lines
.GroupBy(l => l.ProductCode)
.Select(cl => new Models.ResultLine
{
ProductName = cl.select(x=>x.Name).FirstOrDefault(),
Quantity = cl.Count().ToString(),
Price = cl.Sum(c => c.Price).ToString(),
}).ToList();