LINQ查询获得COUNT [英] LINQ query to get COUNT

查看:109
本文介绍了LINQ查询获得COUNT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下表格结构
PartID PartInstanceID PartInstanceName使用
1 1 A Y
1 2 B N
1 3 C N
1 4 D N
2 5 E N
2 6 F N
3 7 G Y
4 8 H N
5 9 I N
5 10 J N


有人可以在LINQ中告诉我查询以获取以下输出吗?

获取未使用的实例:
PartID InstanceCount
1 3
2 2
3 0
4 1
5 2

要获取二手实例:
PartID InstanceCount
1 1
2 0
3 1
4 0
5 0

请让我知道.

预先感谢.

Consider following table structure
PartID PartInstanceID PartInstanceName Used
1 1 A Y
1 2 B N
1 3 C N
1 4 D N
2 5 E N
2 6 F N
3 7 G Y
4 8 H N
5 9 I N
5 10 J N


Can anyone tell me a query in LINQ to get following output?

To get Unused instances:
PartID InstanceCount
1 3
2 2
3 0
4 1
5 2

To get used instances:
PartID InstanceCount
1 1
2 0
3 1
4 0
5 0

Please let me know.

Thanks in advance.

推荐答案

var query = from p in myTable
            where p.Used = "N"
            group p by p.PartID into gp
            select new
                   {
                       PartID = gp.PartID,
                       InstanceCount = gp.Count(),
                   }



我将留给您更改上面的内容,以获取未使用零件的结果.



I''ll leave it to you to alter the above to get the result for unused parts.


这非常简单.看看这个

This is very simple. Have a look with this

var counts = products.GroupBy(l => l.PartID).Select(lg => new { PartID = lg.Key, InstanceCount = lg.Count() });    



这里的产品是一个列表对象.您可能会认为您在列表中收集了数据.



Here products is a list object. you might think that the collection of your data in a List.


这篇关于LINQ查询获得COUNT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆