[英]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
而不是在構造函數中分配 Id 屬性,而是在 PropertyDto 主體中分配 Id 屬性和其他屬性,類似於 MatchingContainsCount 屬性。 EF 核心不會將類構造函數或方法中的復雜屬性分配轉換為 SQL。 只有簡單的任務。 這應該可以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.