Reputation: 18857
Is it possible to order the results of a LINQ join operation based on the inner collection order?
Say I have two collections:
var names = new[]{"John", "Mary", "David"};
var persons= new[]{ new Person{Name="John", Title"Prof"}, new Person{Name="Mary", Title="Accountant"}, new Person{Name="David", Title="Mechanic"}, new Person{Name="Peter", Title="Homeless"}}
if I do a LINQ join to get a subset of persons as follows:
var taxPayers =
persons
.Join(names , p => p.Name, n => n, (p, n) => p)
.Select(f => new KeyValuePair<string, object>(f.Name, f.Title));
The result is ordered based on the persons array.
It is possible using LINQ, to order taxPayers based on the order of names? Or is this not a LINQable operation?
TIA.
Upvotes: 1
Views: 277
Reputation: 11214
Simply reversing the join should work. Instead of joining names to persons, join persons to names.
Upvotes: 2