排序在LINQ一个IEnumerable [英] Sorting an IEnumerable in LINQ

查看:115
本文介绍了排序在LINQ一个IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何给出的例子进行排序



 的IEnumerable<&额外GT; ELIST =新的List<额外>()
{
新的额外{ID = 1,文本=A},
新的额外{ID = 2,文本=G},
新的额外{ID = 3,文字=我},
新的额外{ID = 4,文本=E},
新的额外{n = 5,文本= F},
新的额外{ID = 6,文本=​​D},
新的额外{ID = 7,文本=​​C},
新的额外{ID = 8 ,文本=H},
新的额外{ID = 9,文本=b}
};

的IEnumerable<样品> SAM =新的List<样品>()
{
新样本{ID = 1,名称=样品1,名单=新名单,LT; INT> {1,5,6}}
新样本{ID = 2,名称=样品1,名单=新名单,LT; INT> {2,9}},
新样本{ID = 3,NAME =样品1,名单=新的List< INT> {8,3,7}},
新样本{ID = 4,NAME =样品1,名单=新名单,LT; INT> {3,4,8}}
新样本{n = 5,NAME =样品1,名单=新名单,LT; INT> {1,5,7}},
新样本{ID = 6,NAME =样品1名单=新名单,LT; INT> {6,9,7}}
};



我有这样的代码进行排序,并加入到样本列表上面的额外的对象。



  VAR S2 =(从SAM 
D1选择新的
{
NAME = d1.name,
ID = d1.id,
名单=
(从d1.list
D2
在ELIST加入E在D2等于e.id
选择新{
ID = D2,文字= e.text
}
).OrderBy(项目=> item.text.FirstOrDefault())
});



上面的代码工作正常,但它加入了两个数据和排序的值列表。但我要的是上面的'S2'的输出将再次通过其名单的价值由'list.text进行排序。



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



  {ID = 1 ,NAME =样品1,名单= {'一','F','D'}},
{n = 5,NAME =样品1,名单= {'A​​','F ','C'}},
{ID = 2,名字=样品1,列表= {'G','b'}},
{ID = 4,名字=样品1,名单= {'我','E','H'}},
{ID = 6,NAME =样品1,名单= {'D','b','C' }},
{ID = 3,NAME =样品1,名单= {'H','我','C'}},

在LINQ这可能吗?



感谢


解决方案

  VAR newsam = sam.Select(S = GT;新的
{
ID = s.id,
NAME = s.name,
名单= s.list
。选择(L => eList.FirstOrDefault(E => e.id == L)的.text)
.OrderBy(E => E)
.ToList()
}
).OrderBy(S = GT; s.list.FirstOrDefault())
.ToList();

修改



所以,内部列表由ELIST的文本值排序;和外部列表由内部列表



修改



的第一个元素进行排序

  VAR S2 =(从SAM 
D1选择新的
{
NAME = d1.name,
ID = d1.id,
名单=
($ b $从d1.list
D2 b加入E在ELIST上D2等于e.id
选择e.text
).OrderBy (项目=>项目).ToList()
})排序依据(项目=> item.list.FirstOrDefault());


How to sort the given examples.

        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}}
        };

I have this code to sort and join the sample list to the extra object above.

           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())
                  });

The code above works fine, it joined the two data and sorted the values for the list. But what I want is the output above 's2' will be sorted again by its 'list' value by 'list.text'.

So possible output above must be:

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

Is this possible in LINQ?

thanks

解决方案

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();

EDIT

So, the inner lists are sorted by the text value of the eList; and the outer list is sorted by the first element of the inner list

EDIT

          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());

这篇关于排序在LINQ一个IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆