Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting an IEnumerable in LINQ

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

like image 520
user1120260 Avatar asked Oct 07 '22 18:10

user1120260


1 Answers

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());
like image 118
horgh Avatar answered Oct 17 '22 10:10

horgh