LINQ表达式中GroupBy和where的翻译问题

huangapple go评论53阅读模式
英文:

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 =&gt; item.OwnerId != null)
        .GroupBy(item =&gt; item.OwnerId)
        .Count(grouping =&gt; grouping.Count() &gt; 1);
}

But the method to actually get a list of them fails to translate:

public IEnumerable&lt;IGrouping&lt;Guid?, ItemType&gt;&gt; GetItemsWithDuplicateOwners()
{
    return GetItemsSecureAsync().Result
        .Where(item =&gt; item.OwnerId != null)
        .GroupBy(item =&gt; item.OwnerId)
        .Where(grouping =&gt; grouping.Count() &gt; 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&lt;T&gt; NonDistinct&lt;T, TKey&gt;(this IQueryable&lt;T&gt; source, Expression&lt;Func&lt;T, TKey&gt;&gt; keySelector)
{
    return source.GroupBy(keySelector).Where(g =&gt; g.Count() &gt; 1).SelectMany(r =&gt; 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&lt;ItemType&gt; GetItemsWithDuplicateOwners()
{
    var duplicates = GetItemsSecureAsync().Result
                     .Where(item =&gt; item.OwnerId != null)
                     .GroupBy(item =&gt; item.OwnerId)
                     .Where(grouping =&gt; grouping.Count() &gt; 1);

    var query = duplicates.SelectMany(grouping =&gt; grouping);

    return query.ToList();
}

huangapple
  • 本文由 发表于 2023年4月19日 16:43:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定