none
How to get additional column after select min value RRS feed

  • Question

  • Hi,

    I have the following problem but I don't how I can write LINQ to get what I need.  Please help by posting code here. Thanks

    col1   col2   col3

    n1       23     25

    n2       24     23

    for n1, col2 - col3 = 23 - 25 = abs(-2) = 2

    for n2, col2 - col3 = 24 - 23 =abs(1) = 1

    since 1 is the smallest so I pick n2.  I want to get values from all three columns

    result should be: n2, 24, 23. 

    Tuesday, February 25, 2014 4:35 AM

Answers

  • Hello,

    Have a try to use codes as:

    List<OrderDTO> os = new List<OrderDTO>() { new OrderDTO() { C1 = "N1", C2 = 23, C3 = 25 }, new OrderDTO() { C1 = "N2", C2 = 24, C3 = 23 } };
    
    
                    var result = (from od in os
    
                                  let d = od.C2 > od.C3 ? od.C2 - od.C3 : od.C3 - od.C2
    
                                  orderby d
    
                                  select od).FirstOrDefault();
    

    It will return the N2 record:

    If you want to know more about LINQ, you can refer to link below:

    http://www.codeproject.com/Tips/590978/LINQ-Tutorial-for-Beginners

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 26, 2014 2:35 AM
    Moderator