对于LINQ来说还很陌生,并且认为我想要做的事情应该很容易。我有一个产品列表(ProductId
,ProductDesc
),我正尝试提取该列表的一个子集并将其分组ProductId
。从那里,我想将该子集绑定到listView。这是我正在处理的查询:
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();
这是我要填充的类:
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;
}
}
我只显示了productId
和计数就可以正常工作,但是我也需要在显示中添加说明。我对进行分组时如何添加产品说明感到困惑。
我假设每个名称只有一个,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
并指定如何选择它
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句