According to Pro LINQ: Language Integrated Query in C# 2008, Prototype of OrderBy operator is
public static IOrderedEnumerable<T> OrderBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector)
where
K : IComparable<K>
But the MSDN documentation does not have a generics contraint on TKey that it should be of type IComparable<TKey>
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
I am basically sorting Inventory by Unit and then by Size.
var sortedInventories = inventories
.OrderBy(inventory => inventory.Unit)
.OrderBy(inventory => inventory.Size);
From the above code snippet, lambda expressions simply return inventory properties to sort by. It does not look like an expression that returns IComparer<T>
But according to logic, it looks like the lambda expression should be of type IComparer<T>
.
Which one is the correct declaration of OrderBy
?
(Apress.com Errata page has no information on it)
Here is a sample application I created to test OrderBy
public class Program
{
public static void Main(string[] args)
{
var inventories = new[] {
new Inventory { Unit = 1, Size = 2 },
new Inventory { Unit = 2, Size = 4 },
new Inventory { Unit = 3, Size = 6 },
};
var sortedInventories = inventories
.OrderBy(inventory => inventory.Unit)
.OrderBy(inventory => inventory.Size);
foreach (var inventory in sortedInventories)
Console.WriteLine("Unit: {0}; Size = {1}", inventory.Unit, inventory.Size);
}
}
public class Inventory
{
public int Unit { get; set; }
public double Size { get; set; }
}