List<MyObject> objects = (from a in alist
join b in blist on a.ID equals b.ID
where a.Number != 4
orderby b.Rank, a.CustomField
select a).ToList();
这是我的查询,我想为CustomField属性使用自定义Comparer。有没有办法以双场顺序执行此操作?
我能够做到这一点:
List<MyObject> objects = objects.OrderBy(a => a.CustomField, new MyComparer<object>())
但是我需要它按s.Rank和a.CustomField排序。
答案 0 :(得分:8)
将OrderBy()
与ThenBy()
一起用于您的自定义比较器。
// I'm pretty sure it is not possible to specify
// your custom comparer within a query expression
List<MyObject> objects = (from a in alist
join b in blist on a.ID equals b.ID
where a.Number != 4
select new { a, b })
.OrderBy(o => o.b.Rank, new MyRankComparer())
.ThenBy(o => o.a.CustomField, new MyComparer<object>())
.Select(o => o.a)
.ToList();
答案 1 :(得分:5)
试试这个:
List<MyObject> objects = objects
.OrderBy(a => a.Rank)
.ThenBy(a =>
a.CustomField, new MyComparer<object>()
).ToList();
首先按Rank
字段排序,然后按CustomField
排序自定义比较器。