简体   繁体   中英

How do you order by Greatest number in linq

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; }
}
var data = products.OrderByDescending(x => x.Total);

Change:

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

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM