繁体   English   中英

按匹配相关数据Linq的计数排序,ThenBy不起作用

[英]Sorting by count of matching related data Linq, ThenBy doesnt work

我编写了一个查询,根据给定列表的匹配元素数量对结果进行排序。 这在使用单个 OrderBy 时有效。

但是,由于我想使用分页,我需要使用 ThenBy 来确保顺序始终相同。

当前查询以某种方式将子查询移动到 OrderBy/ThenBy 内并且无法翻译。

如何重新编写此查询以便 ThenBy 可以工作?

谢谢。

代码:

    products
        .Select(product => new ProductDto(product)
        {
            MatchingContainsCount = (from contain in product.Contains
                where allergens.Contains(contain.Allergen.Name)
                select contain.Allergen).Count(),
            MatchingMayContainCount = (from mayContain in product.MayContain
                where allergens.Contains(mayContain.Allergen.Name)
                select mayContain.Allergen).Count()
        })
        .OrderBy(dto => dto.MatchingContainsCount)
        .ThenBy(dto => dto.Id); // Without this line it works

翻译错误: linq 错误 第2部分

而不是在构造函数中分配 Id 属性,而是在 PropertyDto 主体中分配 Id 属性和其他属性,类似于 MatchingContainsCount 属性。 EF 核心不会将类构造函数或方法中的复杂属性分配转换为 SQL。 只有简单的任务。 这应该可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM