OrderBy异常“至少一个对象必须实现IComparable". [英] OrderBy exception "At least one object must implement IComparable"
本文介绍了OrderBy异常“至少一个对象必须实现IComparable".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对列表进行排序.
I'm trying to sort a list.
此行引发异常:;
List<DistanceInfo> ListedDuration =
Distances.OrderBy(o => o.Rows[0].Elements.OrderBy(e => e.Distance))
.ToList();
但是,这可行:
List<DistanceInfo> ListedDuration =
Distances.OrderBy(o => o.Rows[0].Elements[0].Distance)
.ToList();
我该如何解决?
推荐答案
让我们看一下:
List<DistanceInfo> ListedDuration = Distances.OrderBy(
o => o.Rows[0].Elements.OrderBy(e => e.Distance)
).ToList();
我们要订购的东西是:
o => o.Rows[0].Elements.OrderBy(e => e.Distance)
这就是说;对于每个o
项,请按序列进行排序,并按距离o.Rows[0].Elements.OrderBy(e => e.Distance)
进行排序.那没有多大意义.您如何比较两个序列,例如{1,3,4}
和{1,8}
?
which is to say; for each item o
, order it by the sequence ordered by distance o.Rows[0].Elements.OrderBy(e => e.Distance)
. That doesn't make much sense. How do you compare two sequences, such as {1,3,4}
to {1,8}
?
但是,您可以采用 first 距离,或 min / max 距离:
You might, however, take the first distance, or the min / max distance:
List<DistanceInfo> ListedDuration = Distances.OrderBy(
o => o.Rows[0].Elements[0].Distance
).ToList();
List<DistanceInfo> ListedDuration = Distances.OrderBy(
o => o.Rows[0].Elements.Min(e => e.Distance)
).ToList();
List<DistanceInfo> ListedDuration = Distances.OrderBy(
o => o.Rows[0].Elements.Max(e => e.Distance)
).ToList();
这篇关于OrderBy异常“至少一个对象必须实现IComparable".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文