你如何通过linq中的最大数字订购

时间:2011-05-16 04:59:07

标签: c# linq

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

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

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; }
}

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

变化:

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

为:

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