![](/img/trans.png)
[英]C# Entityframework core 2.1 query not including where condition
[英]EntityFramework count members with condition C#
我有一个称为LiquidatorRepresentative的表。 该表包含代表成员ID。 我不知道如何使用EntityFramework或LinQ计算LiquidatorRepresentative中有多少条目在C#中具有相同的RepresentedMemberId
提前致谢。
编辑:我已经做了一个列表InfoList,现在的问题是检查是否有Type = 15和相同RepresentedMemberId的条目。 我无法使用存储过程,因为由于linq,程序已经在缓慢运行。
基本上,您需要的是-按代表会员ID对数据进行分组,然后对每个组中的项目进行计数。 代码将类似于:
var groups = liquidatorRepresentative.GroupBy(lr => lr.RepresentedMemberId).Select(group => new { Id = group.Key, Count = group.Count() })
foreach (var group in groups)
{
Console.WriteLine("{0} {1}", group.Id, group.Count);
}
如果要计算满足条件的实体数,则可以简单地计算:
var count = await (from x in set
where condition(x)
select x).CountAsync();
这将被转换为数据库上的select count(1)...
:它将仅返回计数,而不是所有数据。
Groupby扩展方法应该可以解决问题
var groupCount = db.LiquidatorRepresentative.GroupBy(info => info.RepresentedMemberId)
.Select(group => new {
RepresentedMemberId = group.Key,
Count = group.Count()
});
谢谢您的帮助。 我列出了需要检查的内容,而不是forEach,而是将第一个条目引入了element。 此代码适用于我需要的内容,感谢您的帮助。
if(InfoList.Where(adx => adx.Id == element.Id && adx.Type == "15").Count() > 1
|| InfoList.Where(adx=adx.Id == element.Id && adx.Type == "16").Count() > 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.