Eni*_*ate 19 c# linq linq-to-entities entity-framework
我有一个products用列调用的表:
productid ,
productname,
productprice
categoryid
Run Code Online (Sandbox Code Playgroud)
我的问题是我想根据产品名称和细节获得产品数量.我想显示数据DataGridView.我如何知道单个产品名称的产品数量,如下所示?
productid productname productavailable productprice
--------------------------------------------------------------------------
1 product A 2 products(product A) 100
2 product B 5 Products(product B) 200
Run Code Online (Sandbox Code Playgroud)
像上面的表我必须显示DataGridView.我正在使用LINQ和C#,我的DbContext名字是tsgdbcontext.
tva*_*son 41
GroupBy与包含分组属性的键一起使用.然后select输出关键属性以及分组中每个属性的计数.
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()
});
Run Code Online (Sandbox Code Playgroud)
bli*_*ins 31
不确定我是否完全理解+做出一些假设,但这里是一个示例linq查询,它根据一些任意选择标准(id = 2且价格大于100)产生一个计数...
int count = (from p in tsgdbcontext.Products
where p.productid == 2 && p.productprice > 100
select p).Count();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71016 次 |
| 最近记录: |