英文:
LINQ expression translation issue with GroupBy and where
问题
I am trying to get all items in database that have a duplicate owner. My first method returns their count and works without any issue:
我正在尝试获取数据库中所有具有重复所有者的项目。我的第一种方法返回它们的计数,并且没有任何问题:
public int GetUsersWithMultipleItemsCount()
{
return GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Count(grouping => grouping.Count() > 1);
}
But the method to actually get a list of them fails to translate:
但是实际获取它们列表的方法无法翻译:
public IEnumerable<IGrouping<Guid?, ItemType>> GetItemsWithDuplicateOwners()
{
return GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Where(grouping => grouping.Count() > 1)
.ToList();
}
How can I modify this to work without needing client-side evaluation? I would rather not slow down my app because of such insignificant feature.
如何修改以使其在不需要客户端评估的情况下工作?我宁愿不因为如此微不足道的功能而减慢我的应用程序速度。
Edit: I though the solution worked but now it is failing again despite the SelectMany clause. I have created a small extension function for this purpose:
编辑:我以为解决方案已经可以运行了,但尽管使用了SelectMany子句,现在它再次失败了。我为此创建了一个小的扩展函数:
public static IQueryable<T> NonDistinct<T, TKey>(this IQueryable<T> source, Expression<Func<T, TKey>> keySelector)
{
return source.GroupBy(keySelector).Where(g => g.Count() > 1).SelectMany(r => r);
}
It seems there is an issue with EF Core7 related to this: https://github.com/dotnet/efcore/issues/28002
似乎与 EF Core 7 相关的问题:https://github.com/dotnet/efcore/issues/28002
英文:
I am trying to get all items in database that have a duplicate owner. My first method returns their count and works without any issue:
public int GetUsersWithMultipleItemsCount()
{
return GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Count(grouping => grouping.Count() > 1);
}
But the method to actually get a list of them fails to translate:
public IEnumerable<IGrouping<Guid?, ItemType>> GetItemsWithDuplicateOwners()
{
return GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Where(grouping => grouping.Count() > 1)
.ToList();
}
How can I modify this to work without needing client-side evaluation? I would rather not slow down my app because of such insignificant feature.
Edit: I though the solution worked but now it is failing again despite the SelectMany clause. I have created a small extension function for this purpose:
public static IQueryable<T> NonDistinct<T, TKey>(this IQueryable<T> source, Expression<Func<T, TKey>> keySelector)
{
return source.GroupBy(keySelector).Where(g => g.Count() > 1).SelectMany(r => r);
}
It seems there is an issue with EF Core7 related to this: https://github.com/dotnet/efcore/issues/28002
答案1
得分: 3
这是您提供的代码的中文翻译:
代码中的问题出在 ToList()
上。
首先使用 SelectMany()
。
public IEnumerable<ItemType> GetItemsWithDuplicateOwners()
{
var duplicates = GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Where(grouping => grouping.Count() > 1);
var query = duplicates.SelectMany(grouping => grouping);
return query.ToList();
}
英文:
It fails because of ToList()
Use SelectMany()
first
public IEnumerable<ItemType> GetItemsWithDuplicateOwners()
{
var duplicates = GetItemsSecureAsync().Result
.Where(item => item.OwnerId != null)
.GroupBy(item => item.OwnerId)
.Where(grouping => grouping.Count() > 1);
var query = duplicates.SelectMany(grouping => grouping);
return query.ToList();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论