在LINQ中对IEnumerable进行排序

时间:2012-09-05 07:27:13

标签: c# linq sorting ienumerable

  

可能重复:
  Sorting an IEnumerable in LINQ

是否可以通过不使用.ToList()?

对此进行排序

请参阅以下代码。

以下代码将导致此输出。

    { id = 1, name = "sample 1", list = {'a','f','d'}},         
    { id = 5, name = "sample 1", list = {'a','f','c'}},
    { id = 2, name = "sample 1", list = {'g','b'}},
    { id = 4, name = "sample 1", list = {'i','e','h'}},
    { id = 6, name = "sample 1", list = {'d','b','c'}},
    { id = 3, name = "sample 1", list = {'h','i','c'}},

由于

RJ

IEnumerable<extra> eList = new List<extra>()
{
    new extra{ id = 1, text = "a"},
    new extra{ id = 2, text = "g"},
    new extra{ id = 3, text = "i"},
    new extra{ id = 4, text = "e"},
    new extra{ id = 5, text = "f"},
    new extra{ id = 6, text = "d"},
    new extra{ id = 7, text = "c"},
    new extra{ id = 8, text = "h"},
    new extra{ id = 9, text = "b"}
};

IEnumerable<sample> sam = new List<sample>()
{
    new sample{ id = 1, name = "sample 1", list = new List<int>{1,5,6}},
    new sample{ id = 2, name = "sample 2", list = new List<int>{2,9}},
    new sample{ id = 3, name = "sample 3", list = new List<int>{8,3,7}},
    new sample{ id = 4, name = "sample 4", list = new List<int>{3,4,8}},
    new sample{ id = 5, name = "sample 5", list = new List<int>{1,5,7}},
    new sample{ id = 6, name = "sample 6", list = new List<int>{6,9,7}}
};

var sorted = (from d1 in sam
              select new
              {
                  name = d1.name,
                  id = d1.id,
                  list =
                  (
                      from d2 in d1.list
                      join e in eList on d2 equals e.id
                      select e.text
                  ).OrderBy(item => item).ToList()
              }).OrderBy(item => item.list.FirstOrDefault());

3 个答案:

答案 0 :(得分:0)

<击> 也许ThenBy会这样做?

var sorted = (from d1 in sam 
              select new 
              { 
                  name = d1.name, 
                  id = d1.id, 
                  list = 
                  ( 
                      from d2 in d1.list 
                      join e in eList on d2 equals e.id 
                      select e.text 
                  ).OrderBy(item => item 
              }).ThenBy(item => item.list.FirstOrDefault()); 

<击>

我误解了这个问题。为什么不删除ToList?您可能希望使用.ToList()以防止每次访问集合时进行排序。

答案 1 :(得分:0)

我刚刚测试了没有'ToList'的原始代码,它对项目和'extras'进行了排序就好了。你能详细说明你想要实现的目标吗?证明:

带ToList的原始代码的结果:

1  "sample 1"  a d f  
5  "sample 5"  a c f  
2  "sample 2"  b g  
6  "sample 6"  b c d  
3  "sample 3"  c h i  
4  "sample 4"  e h i  

没有ToList的代码结果:

1  "sample 1"  a d f   <-- adf is sorted
5  "sample 5"  a c f   <-- also sorted
2  "sample 2"  b g     <-- and here too
6  "sample 6"  b c d   <-- yep
3  "sample 3"  c h i   <-- it is!
4  "sample 4"  e h i   <-- ...
               ^
               |
               \ aabbce is sorted, too

看起来和我一样:)

至于一般的想法,在没有“ToList”的情况下用LINQ语法编写的小问题是JOIN将被懒惰地执行,每个项目一次,并且每次它将从头开始构建映射/连接,而它是完全可缓存和可重用的。下面,在扩展语法中,我只显式地预创建映射一次,然后所有其他没有任何实现的惰性查询共享相同的映射。这样它可以快几倍并且使用更少的内存:

var mapping = eList.ToDictionary(x => x.id, x=>x.text);

var temps = sam.Select(s =>
    new {
        id = s.id,
        name = s.name,
        stringlist = s.list.Select(id => mapping[id]).OrderBy(str => str)
    });

var result = temps.OrderBy(t => t.stringlist.FirstOrDefault());

答案 2 :(得分:0)

试试这个:

var sorted = (from d1 in sam
              select new
              {
                  name = d1.name,
                  id = d1.id,
                  list =
                  (
                      from d2 in d1.list
                      join e in eList on d2 equals e.id
                      select e.text
                  )
              }).OrderBy(item => item.list.OrderBy(i => i).FirstOrDefault());