C# Linq OrderByDescending Contains

  • 170
  • 0
  • C#
  • 2026-11-24

筆記下按照某 bool 條件 (Id是否包含在指定清單中) 來排序集合

想把「Id 包含在指定清單裡」的資料排到前面,可以用 OrderByDescending(x => idList.Contains(x.Id))。

符合條件的結果是 true,會排在 false 前面;不符合的資料仍會保留在後面。

完整文章與更新版本已搬到 jakeuj.com:C# Linq OrderByDescending Contains

PS5

本部落格已搬到 jakeuj.com,文章在新站持續更新。