按匹配相关的数据linq进行排序,而不是工作
我编写了一个查询,根据给定列表的匹配元素的数量对结果进行分类。当使用单个订单时,这起作用。
但是,由于我想使用分页,因此我需要使用当时的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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是在构造函数内分配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.