user3399655
user3399655

Reputation: 35

Find the product with the lowest price return with name LINQ

I´m new to LINQ and I´m trying to find the lowest price in a list and return the name of it.

I´ve been searching and haven´t find anything that I can use.

The List is in a class Category but I have to write out the result in main.

It´s a C# in a Microsoft Visual Studio.

The list I have to find the lowest price from is like this:

public static IEnumerable<Product> GetProducts( )
    {
        List<Product> products = new List<Product>( );
        products.Add( new Product { Name = "Milk",           Price = 90,       CategoryID = 4, ID = 1 } );
        products.Add( new Product { Name = "Cheese",         Price = 130, CategoryID = 4, ID = 2 } );
        products.Add( new Product { Name = "Butter",         Price = 110, CategoryID = 4, ID = 3 } );

        products.Add( new Product { Name = "Apple juice",    Price = 230, CategoryID = 1, ID = 4 } );
        products.Add( new Product { Name = "Grape juice",    Price = 240, CategoryID = 1, ID = 5 } );
        products.Add( new Product { Name = "Beetroot juice", Price = 300, CategoryID = 1, ID = 6 } );
        products.Add( new Product { Name = "Carrot juice",   Price = 190, CategoryID = 1, ID = 7 } );
        products.Add( new Product { Name = "Ginger ale",     Price = 990, CategoryID = 1, ID = 8 } );

        products.Add( new Product { Name = "Oregano",        Price = 500, CategoryID = 2, ID = 9 } );
        products.Add( new Product { Name = "Salt",           Price = 550, CategoryID = 2, ID = 10 } );
        products.Add( new Product { Name = "Pepper",         Price = 490, CategoryID = 2, ID = 11 } );

        products.Add( new Product { Name = "Carrots",        Price = 300, CategoryID = 3, ID = 12 } );
        products.Add( new Product { Name = "Spinach",        Price = 250, CategoryID = 3, ID = 13 } );
        products.Add( new Product { Name = "Onion",          Price = 200, CategoryID = 3, ID = 14 } );
        products.Add( new Product { Name = "Garlic",         Price = 150, CategoryID = 3, ID = 15 } );
        products.Add( new Product { Name = "Tomatoes",       Price = 100, CategoryID = 3, ID = 16 } );

        return products;
    }

Upvotes: 1

Views: 2398

Answers (4)

Marco
Marco

Reputation: 23937

EDIT I've changed the answer after Tim Schmelters comment on Ralph Shillingtons answer, which is very similiar to mine, but in a different syntax.

int lowestPrice = from prod in GetProducts()
                  select prod.Price).Min();                             
var lowestPriceProduct = from p in GetProducts()
                         where lowestPrice == p.Price)
                         select p;

Upvotes: 0

Ralph Shillington
Ralph Shillington

Reputation: 21098

from p in Products where p.Price == Products.Min(x=>x.Price) select p.Name

The problem with taking the First from an Ordered list is that it doesn't deal with the possibilities of multiple items having the same lowest price.

Upvotes: 4

Tim Schmelter
Tim Schmelter

Reputation: 460058

This returns Milk

string namesOfProductWithLowestPrice =  products
        .GroupBy(p => p.Price)
        .OrderBy(g => g.Key)
        .Select(g => string.Join(",",  g.Select(p => p.Name)))
        .FirstOrDefault();

In case of multiple products with the lowest price it will concatenate the names with comma.

Upvotes: 1

thepirat000
thepirat000

Reputation: 13114

products.OrderBy(p => p.Price).Select(p => p.Name).First();

or

products.OrderBy(p => p.Price).First().Name;

Upvotes: 3

Related Questions