locked
LINQ - How to Return true if value exists between two column in a table. RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    How to check value in between two column of the datatable.

    For example,

    My input value 10.

    Atable Btable
    2 8
    4 10
    7 11
    10 6
    10 20
    6 20
    5 21

    Now the input value should find in between A and B Table.

    If exists within the limit, then it should return true else false.

    How to write linq query.

    my query is below. but, it is not working.

     var Result6 = (from r in tblRes1.AsEnumerable()
                                    where r.Field<string>("FROM DIA") <= PipeDia && r.Field<string>("TO DIA") >= PipeDia  // Pipe Diameter
                                    select new
                                   {

                                   }).Count();

    Error : Operator '<=' cannot be applied to operands of type 'sting' and 'string'

    what is the problem in that..?

    Friday, June 30, 2017 11:25 AM

Answers

  • User1068175894 posted

    convert the strings to int:

    var Result6 = (from r in tblRes1.AsEnumerable()
                                    where Convert.ToInt32(r.Field<string>("FROM DIA")) <= PipeDia && Convert.ToInt32(r.Field<string>("TO DIA")) >= PipeDia  // Pipe Diameter
                                    select new
                                   {
    
                                   }).Count();

    Im not converting PipeDia to int as I assume you can change the variable type:

    int PipeDia = 10;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 30, 2017 1:13 PM

All replies

  • User1068175894 posted

    convert the strings to int:

    var Result6 = (from r in tblRes1.AsEnumerable()
                                    where Convert.ToInt32(r.Field<string>("FROM DIA")) <= PipeDia && Convert.ToInt32(r.Field<string>("TO DIA")) >= PipeDia  // Pipe Diameter
                                    select new
                                   {
    
                                   }).Count();

    Im not converting PipeDia to int as I assume you can change the variable type:

    int PipeDia = 10;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 30, 2017 1:13 PM
  • User-832373396 posted

    Hi gani7787,

    May I know "FROM DIA" is your column name ??? I guess it should be "DIA" or "FROM_DIA", etc. please have a check.

    where Convert.ToInt32(r.Field<string>("FROM DIA")) <= PipeDia && Convert.ToInt32(r.Field<string>("TO DIA")) >= PipeDia // Pipe Diameter

    @JBetancourt, why not use code :where r.Field<int>("FROM DIA")<= PipeDia && r.Field<int>("TO DIA")>= PipeDia   directly?

    With Regards,

    Angelina Jolie


                                   

    Monday, July 3, 2017 6:48 AM