我怎么能得到在linq的计数 [英] how can i get the count in linq
本文介绍了我怎么能得到在linq的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为的产品
与列:
productid ,
productname,
productprice
categoryid
我的问题是我想要根据产品名称以及详细信息获取产品数量。我想在 DataGridView
中显示数据。如何知道以下单一产品名称的产品数量?
My problem is I want to get the number of products depending on product name along with details. I want to show the data in DataGridView
. How can I know the number of products for a single product name like below?
productid productname productavailable productprice
--------------------------------------------------------------------------
1 product A 2 products(product A) 100
2 product B 5 Products(product B) 200
如上表所示,我必须显示在 DataGridView
。我使用LINQ和C#,而我的 DbContext
名称是 tsgdbcontext
。
Like the above table I have to display in DataGridView
. I am using LINQ and C# and my DbContext
name is tsgdbcontext
.
推荐答案
使用 GroupBy
包含分组属性的键。然后选择
关键属性以及分组中的每个的计数。
Use GroupBy
with a key that contains your grouping properties. Then select
out the key properties along with the count of each from the grouping.
var query = tsgdbcontext.Products
.GroupBy(p => new {
p.ProductId,
p.ProductName,
p.ProductPrice
})
.Select(g => new {
g.Key.ProductId,
g.Key.ProductName,
g.Key.ProductPrice,
Available = g.Count()
});
这篇关于我怎么能得到在linq的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文