Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ- Combine Multiple List<T> and order by a value (.Net 3.5)

Tags:

I'm trying to combine a number of List<T> where T:IGetTime (i.e T will always have method getTime()).

Then I'm trying order the items by the DateTime that getTime() returns.

My LINQ looks like this:

public static List<T> Combine(List<T> one, List<T> two, List<T> three)     {         var result = from IGetTime item in one                      from IGetTime item2 in two                      from IGetTime item3 in three                      select new{ item, item2, item3 };          return result.ToList();     } 

I've yet to add an orderby. Which should look somthing like this:

var thestream = from T item in this                 orderby item.getTime() descending                 select item; 

Is there anyway to both combine and order the final list????

Thanks in advance,

Roberto

like image 823
Roberto Bonini Avatar asked Dec 28 '08 20:12

Roberto Bonini


1 Answers

You've got three lists here - what exactly are you trying to order by?

Your cast is incorrect too - the result will be an IEnumerable<X> where X is an anonymous type with three Ts in.

If the idea is really to concatenate the three lists and then sort, you want:

return one.Concat(two)           .Concat(three)           .OrderByDescending(x => x.GetTime())           .ToList(); 
like image 109
Jon Skeet Avatar answered Oct 08 '22 08:10

Jon Skeet