none
select highest value among same ones RRS feed

  • Question

  • Hi All,

    I am beginner on linq. my question is maybe simple for most of you , i hope you dont mind.
    I have list of products and each product has code and price. code is not unique. thats why
    i would like to have cheapest product from each group of code.

    for example:

    product code  price
    1            A        1
    2            A        2
    3            B        1
    4            B        2
    5            A        3
    6           C        3

    query must return only product  product 1 3 and 6

    Thanks for help
    Thursday, March 11, 2010 2:00 PM

Answers

  • Hi,

    Something like this may work for you:

    from p in Products
    //where conditions, if any, to be included here
    group p by p.Code into grp
    let MinPricePerProduct = grp.Min ( g=>g.Price )
    
    from p in grp
    where p.Price == MinPricePerProduct 
    select p
    



    This approach is more described in this post.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by emil_tr Thursday, March 11, 2010 5:19 PM
    Thursday, March 11, 2010 2:58 PM
  • I just found the way to write also in VB , here is same code in VB.

        Products = (From p In Product Group p By p.Code Into Group Let CheapestPricePerCode = Group.Min(Function(p) p.Price) _
            From p In Group Where p.Price = CheapestPricePerCode _
            Select p).ToList


    that article also great to see syntax differences between vb and c#

    http://www.aspfree.com/c/a/.NET/Grouping-and-Aggregating-When-Querying-LINQ-to-SQL/3/
    • Marked as answer by emil_tr Thursday, March 11, 2010 5:19 PM
    Thursday, March 11, 2010 5:18 PM

All replies

  • Hi,

    Something like this may work for you:

    from p in Products
    //where conditions, if any, to be included here
    group p by p.Code into grp
    let MinPricePerProduct = grp.Min ( g=>g.Price )
    
    from p in grp
    where p.Price == MinPricePerProduct 
    select p
    



    This approach is more described in this post.

    Regards,
    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by emil_tr Thursday, March 11, 2010 5:19 PM
    Thursday, March 11, 2010 2:58 PM
  • Thanks looks good but what is into grp ? i tried to convert that code in vb and i get compiler error, why do i have to use "into grp" part? is it neccessary ?
    Thursday, March 11, 2010 4:32 PM
  • I just found the way to write also in VB , here is same code in VB.

        Products = (From p In Product Group p By p.Code Into Group Let CheapestPricePerCode = Group.Min(Function(p) p.Price) _
            From p In Group Where p.Price = CheapestPricePerCode _
            Select p).ToList


    that article also great to see syntax differences between vb and c#

    http://www.aspfree.com/c/a/.NET/Grouping-and-Aggregating-When-Querying-LINQ-to-SQL/3/
    • Marked as answer by emil_tr Thursday, March 11, 2010 5:19 PM
    Thursday, March 11, 2010 5:18 PM