按匹配相关的数据linq进行排序,而不是工作

发布于 2025-02-10 16:07:35 字数 1107 浏览 2 评论 0原文

我编写了一个查询,根据给定列表的匹配元素的数量对结果进行分类。当使用单个订单时,这起作用。

但是,由于我想使用分页,因此我需要使用当时的BY来确保订单始终相同。

当前查询以某种方式将子查询移动到订单中/当时,然后无法翻译。

我如何重写此查询以使得By可以工作?

谢谢。

代码:

    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

翻译错误:

I wrote a query that sorts the result based on the amount of matching elements of a given list. This works when using a single OrderBy.

However, since I want to use Pagination, I need to use a ThenBy to make sure the order is always the same.

The current query somehow moves the subquery inside the OrderBy/ThenBy and can't be translated.

How can I re-write this query so that ThenBy would work?

Thanks.

Code:

    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

The Translation error:
linq error
part2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷遇一生 2025-02-17 16:07:36

而不是在构造函数内分配ID属性,将ID属性和其他属性分配在属性属性主体内,类似于MatchingContainsCount属性。 EF Core不会将类构造函数或方法中的复杂属性分配转换为SQL。只有简单的作业。这应该解决问题。

Instead assigning Id property inside the constructor, assign the Id property and other properties within the PropertyDto body, similar to MatchingContainsCount property. EF core doesn't translate complex property assignment within the class constructor or methods to SQL. Only the simple assignments. This should fix the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文