0

I have a collection where i need to find an item with lowest price if more than 1 found the by default any should be selected and it's isPriceSelected property need to set false.

I am trying something like this.

lstBtn.Where(p => p.CategoryID == btnObj.CategoryID && 
                  p.IsSelected == true && p.IsPriceApplied == true)
      .ToList()
      .Min(m=>m.Price)
2
  • Why you want use Linq? Just simply do this: myList.Sort(); then select the first element of the list Commented Nov 25, 2013 at 14:37
  • @Tinwor: because his requirement is to get the minimum price, not to sort the original collection. Apart from that, lstBtn.Sort would not sort by Price automatically. Commented Nov 25, 2013 at 14:49

3 Answers 3

8

Just the select the property that you want the minimum from:

var minimumPrice = lstBtn
   .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied)
   .Min(p => p.Price);

If you actually want to find the item with the lowest price you need to order the collection:

var itemWithMinimumPrice = lstBtn
   .OrderBy(p => p.Price)
   .FirstOrDefault(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied);

or this, could be more efficient:

var itemWithMinimumPrice = lstBtn
   .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied)
   .OrderBy(p => p.Price)
   .FirstOrDefault();

Enumerable.FirstOrDefault returns one item or null if no item matches the predicate.

Sign up to request clarification or add additional context in comments.

7 Comments

Curious minds want to know, could this also be lstBtn.Min(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied);?
@MichaelPerrenoud: That returns false if there is at least one item that does not match this predicate, otherwise true(since true is "higher" than false). But it's obviously not what OP wants
okay so I'd have to return the actual value if I wanted to inline the condition like that; in effect gaining nothing really because it's much more readable and just as efficient with the Where, right?
@MichaelPerrenoud: I must admit that i don't understand your last comment. Min is used to return a minimum value, Where is used to restrict the items used to calculate the min-value, so both are needed. It helps to "translate" the Where to an if-statement (so not an additional loop or method).
it wil return the minimum price itself or the object whose property i can reset to false
|
2

You can try something like this:

var result = lstBtn
    .Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected && p.IsPriceApplied)
    .OrderBy(p => p.Price)
    .First();

This will first find all items which have the specified CategoryID, IsSelected, and IsPriceApplied all set to true, then sort items by Price, and return the first item with the lowest price.

2 Comments

How can i set isPriceApplied to false in the same statement.
The best option is to do it in a second statement result.IsPriceApplied = false
1

Out of the box, linq can only return the actual value with Min and Max methods. You can use a good project morelinq https://code.google.com/p/morelinq/wiki/OperatorsOverview It has the method you need. For myself, I find this project having too many methods, so I simply cut and paste only needed from its sources. With morelinq your code should look like:

lstBtn.Where(p => p.CategoryID == btnObj.CategoryID && p.IsSelected == true && p.IsPriceApplied==true).MinBy(m=>m.Price)

Another approach, if you also need to get all duplicates:

var lowestPriceProducts = lstBtn.Where(p => p.CategoryID == btnObj.CategoryID)
    .GroupBy(p => p.Price, new { p.Price, Product = p})
    .OrderByDescending(x => x.Price)
    .First()
    .Select(x => x.Product)
    .ToList()

This query will return you a list (with only one item if there are no duplicate prices) of products with minimal price. Then you can do anything with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.