Given this code:
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void OrderByEx1()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age == 8 ? 0 : 1);
foreach (Pet pet in query)
{
Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
}
}
Is it possible to extract pet => pet.Age == 8 ? 0 : 1
into its own method and do some conditonal statements based on pet.Age
without using ternary operations?
To add to this, I have tried doing something like this:
IEnumerable<Pet> query = pets.OrderBy(pet => NewMethod(pet));
And define the method as such:
private static Expression<Func<Pet, int>> NewMethod(Pet pet)
{
if(pet.Age == 8)
return 0;
else
return 1;
}
But that does not seem to work as intended.
But doing this:
IEnumerable<Pet> query = pets.OrderBy(NewMethod);
And this, works fine:
private static Expression<Func<Pet, int>> NewMethod(Pet pet)
{
return pet => pet.Age == 8 ? 0 : 1;
}