If you truly only want the Ford Explorers to be grouped, you can do the following:
var groupedByFordExplorer = from c in cars
let isFordExplorer = c.Make == "Ford" && c.Model == "Explorer"
orderby isFordExplorer, c.Year
select c;
What the above query does is create an inline value , isFordExplorer
, and assigns it a boolean value using the let
keyword indicating whether the car is a Ford Explorer. That value can then be sorted by, along with the Year. The following is a working program that should demonstrate the concept:
class Program
{
static void Main(string[] args)
{
var cars = new List<Car>
{
new Car { Year = 2009, Make = "Ford", Model = "Explorer" },
new Car { Year = 2001, Make = "Hummer", Model = "H1" },
new Car { Year = 2002, Make = "Ford", Model = "Focus" },
new Car { Year = 2008, Make = "BMW", Model = "325i" },
new Car { Year = 2008, Make = "Ford", Model = "Explorer" },
new Car { Year = 2008, Make = "Ford", Model = "Escape" },
new Car { Year = 2009, Make = "Mitsubishi", Model = "Galant" },
new Car { Year = 2004, Make = "Ford", Model = "Explorer" },
new Car { Year = 2009, Make = "BMW", Model = "329i" },
new Car { Year = 2003, Make = "Ford", Model = "Explorer" }
};
var groupedByFordExplorer = from c in cars
let isFordExplorer = c.Make == "Ford" && c.Model == "Explorer"
orderby isFordExplorer, c.Year
select c;
foreach (var car in groupedByFordExplorer)
{
Console.WriteLine("{0} {1} {2}", car.Year, car.Make, car.Model);
}
Console.ReadLine();
}
}
class Car
{
public int Year { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}