Pro LINQ 书中的 OrderBy Signature 错误?

发布于 2024-07-16 05:16:52 字数 2124 浏览 4 评论 0原文

根据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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-07-23 05:16:52

将第二个“OrderBy”更改为“ThenBy”。 您当前正在对所有内容进行排序,因此可以按大小和单位进行有效排序,但效率很低。 我不确定您认为 IComparer 应该出现在哪里,除非您将其指定为另一个参数。 基本上它使用 Comparer.Default 除非您指定单独的比较器。

无论如何,您的查询应该是:(

var sortedInventories = inventories
                          .OrderBy(inventory => inventory.Unit)
                          .ThenBy(inventory => inventory.Size);

通过您的测试数据,您无法分辨出差异,因为在每种情况下 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 uses Comparer<T>.Default unless you specify a separate comparer.

Anyway, your query should be:

var sortedInventories = inventories
                          .OrderBy(inventory => inventory.Unit)
                          .ThenBy(inventory => inventory.Size);

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文