我撰寫了一個查詢,根據給定串列的匹配元素數量對結果進行排序。這在使用單個 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
翻譯錯誤:

uj5u.com熱心網友回復:
而不是在建構式中分配 Id 屬性,而是在 PropertyDto 主體中分配 Id 屬性和其他屬性,類似于 MatchingContainsCount 屬性。EF 核心不會將類建構式或方法中的復雜屬性分配轉換為 SQL。只有簡單的任務。這應該可以解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/497407.html
上一篇:檢查記錄是否存在回傳“序列包含多個匹配元素”。對于帶有物體框架5的Net5
下一篇:如何選擇屢次低分的學生?
