LINQ Select and Group By

Pretty new to LINQ and I think what I'm trying to do should be easy. I have a product list ( ProductId, ProductDesc), and I'm trying to pull out a subset of this list and group it using ProductId. From there, I would like to bind this subset to the View list. Here is the query I'm working on:

productCounts = (from record in wowReportData 
                 group record by record.ProductID 
                 into grouping
                 orderby grouping.Key
                 select new topProduct 
                 { 
                     ProductID = grouping.Key, 
                     Quantity = grouping.Count(),
                     Name = grouping.
                 }).ToList();

and here is the class I'm trying to populate:

public class topProduct
{
    public string ProductID { get; set; }
    public int Quantity { get; set; }
    public string Name { get; set; }

    public topProduct() { }

    public topProduct(string productId, string productDesc, int downloadCount)
    {
        this.ProductID = productId;
        this.Name = productDesc;
        this.Quantity = downloadCount;
    }
}

I worked fine just displaying a ProductIdcounter, but I need to add a description to the display as well. I am confused about how to add a product description when I do grouping.

+4
source share
1 answer

, ProductId , 2 :

productCounts = (from record in wowReportData 
                 group record by new { record.ProductID, record.Name } into grouping
                 orderby grouping.Key.ProductID
                 select new topProduct 
                 { 
                     ProductID = grouping.Key.ProductID, 
                     Quantity = grouping.Count(),
                     Name = grouping.Key.Name
                 }).ToList();

, FirstOrDefault ,


, #, ,

+4

Source: https://habr.com/ru/post/1652051/


All Articles