如何使用Linq和Sort处理对象列表?

时间:2017-01-08 06:15:56

标签: c# linq sorting object

很抱歉,如果这是重复的话。我很难找到答案,因为我的搜索结果中有很多人要求如何对列表进行排序。

说我有这样的房产:

public List<object> Hello { get; set; }

假设列表中的每个元素都具有相同的可空原始数据类型。

所以每个元素都是一个int?或者每个元素都是小数?或者每个元素都是DateTime?等等

如果我在此列表中调用Sort或OrderBy会发生什么?它是根据每个元素的基础类型排序还是按属性的字符串值排序?

1 个答案:

答案 0 :(得分:1)

让我们检查http://ideone.com/kcXmHE

System.ArgumentException: Value does not fall within the expected range.
  at System.String.CompareTo (System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Comparer`1+DefaultComparer[System.Object].Compare (System.Object x, System.Object y) [0x00000] in <filename unknown>:0 
  at System.Linq.SortSequenceContext`2[System.Object,System.Object].Compare (Int32 first_index, Int32 second_index) [0x00000] in <filename unknown>:0 
  at System.Array.qsort[Int32] (System.Int32[] keys, Int32 low0, Int32 high0, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.SortImpl[Int32] (System.Int32[] keys, Int32 index, Int32 length, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.Sort[Int32] (System.Int32[] array, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1[System.Object].PerformSort () [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1+<Sort>c__Iterator0[System.Object].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[System.Object]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[Object] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at Test.Main () [0x00000] in <filename unknown>:0 

因此,LINQ对列表元素使用默认比较器(对于字符串值,您可以看到System.String.CompareTo)并尝试与另一个值(对象类型)进行比较。而且,不能这样做。

如果更改初始列表中的元素顺序(http://ideone.com/foljli),您将获得另一个堆栈跟踪:

System.ArgumentException: Object must be of type Int32.
  at System.Int32.CompareTo (System.Object value) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Comparer`1+DefaultComparer[System.Object].Compare (System.Object x, System.Object y) [0x00000] in <filename unknown>:0 
  at System.Linq.SortSequenceContext`2[System.Object,System.Object].Compare (Int32 first_index, Int32 second_index) [0x00000] in <filename unknown>:0 
  at System.Array.qsort[Int32] (System.Int32[] keys, Int32 low0, Int32 high0, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.SortImpl[Int32] (System.Int32[] keys, Int32 index, Int32 length, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Array.Sort[Int32] (System.Int32[] array, IComparer`1 comparer) [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1[System.Object].PerformSort () [0x00000] in <filename unknown>:0 
  at System.Linq.QuickSort`1+<Sort>c__Iterator0[System.Object].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[System.Object]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[Object] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at Test.Main () [0x00000] in <filename unknown>:0 

现在,LINQ采用int32并尝试与object进行比较。再次失败。因为int32类型没有方法CompareTo(object)