One of the common programming problems is sorting a complex object on a list of criteria. For example, given the following class.

public class Car
{
    public int ModelNumber { get; set; }
}

Imagine you wanted to pick out all Cars in your data model that had Model Numbers between 2 and 4? In Linq you would normally do something like this.

var wantedModeles = new int[] { 2, 3, 4};
var sortedCars = Cars.Where(car => wantedModeles.Contains(car.ModelNumber));

This of course works but it doesn’t look very elegant at all. In fact I much prefer the SQL syntax version of this kind of query; using the in operator. So I’ve been thinking about away to make this work a little better. Well I’ve finally come up with a solution that I like. Take a look at this example.

var wantedModeles = new int[] { 2, 3, 4};

var sortedCars2 =
    from car in Cars
    join model in wantedModeles on car.ModelNumber equals model
    select car;

In this case I create use a join operation to sort the wanted sub-list of items. The join operation sorts out all of the items wanted based on the joined properties. I like this syntax a lot more because I can combine it with where clauses that would normally be if/then/else statements after the initial query.