linq中如何按最大数字排序

发布于 2024-11-07 18:18:00 字数 722 浏览 0 评论 0原文

我想按数量顺序打印产品。总量较大的产品应该排在第一位。

我在这里缺少什么,因为它没有按顺序或总计打印

class Program
{
    static void Main()
    {
        var products=new List<Product>
                         {
                             new Product {Name = "Apple", Total = 5},
                             new Product {Name = "Pear", Total = 10}
                         };

        var productsByGreatestQuantity = products.OrderBy(x => x.Total);

        foreach (var product in productsByGreatestQuantity)
        {
            System.Console.WriteLine(product.Name);
        }
        System.Console.Read();
    }
}

public class Product
{
    public string Name { get; set; }
    public int Total { get; set; }
}

I would like to print products in order of quantity.The product with a bigger total should be first.

What am I missing here as it's NOT printing in order or total

class Program
{
    static void Main()
    {
        var products=new List<Product>
                         {
                             new Product {Name = "Apple", Total = 5},
                             new Product {Name = "Pear", Total = 10}
                         };

        var productsByGreatestQuantity = products.OrderBy(x => x.Total);

        foreach (var product in productsByGreatestQuantity)
        {
            System.Console.WriteLine(product.Name);
        }
        System.Console.Read();
    }
}

public class Product
{
    public string Name { get; set; }
    public int Total { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

扎心 2024-11-14 18:18:00
var data = products.OrderByDescending(x => x.Total);
var data = products.OrderByDescending(x => x.Total);
清风疏影 2024-11-14 18:18:00

更改:

var productsByGreatestQuantity = products.OrderBy(x => x.Total);

至:

var productsByGreatestQuantity = products.OrderByDescending(x => x.Total);

Change:

var productsByGreatestQuantity = products.OrderBy(x => x.Total);

to:

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