5

如何对给定的示例进行排序。

        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 1", list = new List<int>{2,9}},
            new sample{ id = 3, name = "sample 1", list = new List<int>{8,3,7}},
            new sample{ id = 4, name = "sample 1", list = new List<int>{3,4,8}},
            new sample{ id = 5, name = "sample 1", list = new List<int>{1,5,7}},
            new sample{ id = 6, name = "sample 1", list = new List<int>{6,9,7}}
        };

我有这个代码来排序并将示例列表加入到上面的额外对象中。

           var s2 = (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 new {  
                             id = d2, text = e.text
                         }
                      ).OrderBy(item => item.text.FirstOrDefault())
                  });

上面的代码工作正常,它连接了两个数据并对列表的值进行了排序。但我想要的是's2'上面的输出将再次按'list.text'的'list'值排序。

所以上面可能的输出必须是:

        { 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'}},

这在 LINQ 中可行吗?

谢谢

4

1 回答 1

4
var newsam = sam.Select(s => new
                 {
                   id = s.id,
                   name = s.name,
                   list = s.list
                           .Select(l => eList.FirstOrDefault(e => e.id == l).text)
                           .OrderBy(e => e)
                           .ToList()
                 }
                ).OrderBy(s => s.list.FirstOrDefault())
                 .ToList();

编辑

因此,内部列表按 eList 的文本值排序;并且外部列表按内部列表的第一个元素排序

编辑

          var s2=(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());
于 2012-09-03T03:53:02.040 回答