Pro LINQ 书中的 OrderBy Signature 错误?
根据Pro LINQ: Language Integrated Query in C# 2008,OrderBy 的原型 运算符是
public static IOrderedEnumerable<T> OrderBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector)
where
K : IComparable<K>
但是 MSDN 文档 确实如此TKey 上没有泛型约束,它应该是 IComparable
类型
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
我基本上按 Unit 对 Inventory 进行排序,然后按 >尺寸。
var sortedInventories = inventories
.OrderBy(inventory => inventory.Unit)
.OrderBy(inventory => inventory.Size);
从上面的代码片段来看,lambda 表达式只是返回库存属性以进行排序。 它看起来不像一个返回 IComparer
的表达式,
但根据逻辑,它看起来应该是 IComparer
类型。
哪一个是 OrderBy
的正确声明?
(Apress.com 勘误页没有相关信息)
这是我创建的用于测试 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; }
}
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将第二个“OrderBy”更改为“ThenBy”。 您当前正在对所有内容进行排序,因此可以按大小和单位进行有效排序,但效率很低。 我不确定您认为
IComparer
应该出现在哪里,除非您将其指定为另一个参数。 基本上它使用Comparer.Default
除非您指定单独的比较器。无论如何,您的查询应该是:(
通过您的测试数据,您无法分辨出差异,因为在每种情况下
Size = Unit * 2
。尝试使用一个具有较小 Unit 和较大 Unit 的项目不过尺寸。)是的,这本书的签名似乎有点错误 - 可能是因为它在发布前不久发生了变化。 如果您基本上担心得到错误的结果,以上是解释。
Change your second "OrderBy" to "ThenBy". You're currently resorting everything, so it's effectively by Size and then Unit, but inefficiently. I'm not sure where you think
IComparer<T>
should come in, unless you specify it as another argument. Basically it usesComparer<T>.Default
unless you specify a separate comparer.Anyway, your query should be:
(With your test data you can't tell the difference, because in each case
Size = Unit * 2
. Try it with one item which has a small Unit and a large Size though.)Yes, it looks like the book got the signature slightly wrong - possibly due to it changing shortly before release. If you were basically worried about getting the wrong results though, the above is the explanation.