user9969
user9969

Reputation: 16040

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

Upvotes: 5

Views: 376

Answers (2)

gotnull
gotnull

Reputation: 27214

Change:

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

to:

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

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33139

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

Upvotes: 8

Related Questions