在我的代碼中,我需要通過價格或Rating.TotalGrade對收集進行排序,正如您可以看到兩個LINQ查詢幾乎是相同的語句,只有很小的差異。在LINQ謂詞中使用OrderBy?
我想使用LINQ的謂詞代替但你可以看到在排序依據的主要區別是,我發現使用排序依據在查詢中沒有樣品。是否有可能或有其他方法來縮短我的代碼,也許未來會有更多的條件。
if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Position)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Price, co.CurrentRanking
select co);
}
else if (CurrentDisplayMode == CRSChartRankingGraphDisplayMode.Grade)
{
this.collectionCompleteSorted = new List<Result>(from co in collection
where co.IsVirtual == false
orderby co.Rating.TotalGrade, co.CurrentRanking
select co);
}
完美的作品!謝謝!早期轉變爲列出壞事? – TalkingCode 2011-04-12 09:07:00
它還不錯。但是如果你太早轉換爲List,你不能使用ThenBy進行二級排序。另外,如果您使用Linq to SQL或Entities,那麼這將導致查詢完全轉換爲SQL。如果您提前轉換爲列表,則會在客戶端完成排序。 – Euphoric 2011-04-12 09:46:54