none
if else condition in LINQ

    Question

  • Asslam-o-Alaikum to all

    can i use if else condition in LINQ,

    OTdataContext dc = new OTdataContext();

    var query = from p in dc.Patients

     

    // i want to do that

    if (p.age > 24)

    {

    //.....

    }

    else

    {

    //........

    }

    Select P;

     

    some one plz help me


    Taha Zubair Ahmed Software Emgineer.
    Tuesday, September 21, 2010 2:27 PM

Answers

  • int result= (from p in dc.Patients
                            select p.PAge.Value).FirstOrDefault();

     Now its ok ....

    use Value Keyword to solve this problems.......


    Taha Zubair Ahmed Software Emgineer.
    • Marked as answer by Larcolais Gong Tuesday, September 28, 2010 5:20 AM
    Saturday, September 25, 2010 12:47 PM

All replies

  • You can use Some thing like this

    var query = from p in dc.Patients
    where (p.age > 24)? (condition if true): (condition if false)
    select p;
    
    Tuesday, September 21, 2010 6:14 PM
  • Thank u for ur Reply

    can u plz define it more clearly syntax......

    thanks once again

     


    Taha Zubair Ahmed Software Emgineer.
    Tuesday, September 21, 2010 7:24 PM
  • It's Ok, I will , but send me you pace of code even in normal English or with bug , I will try to convert it to runnable code.

     

    Welcome

    Tuesday, September 21, 2010 7:26 PM
  • In Stored Procedure we use if Cond. likes that if i want to do in LINQ is that done...

    Declare @Value int

    Select @value= TotalBookingPrice from dbo. Booking where ID= 94685

    if ( @Value < 100)

    Begin

    print 'Blow'

    End

    Else if ( @Value <= 300)

    Begin

    print 'Normal'

    End

    Else if ( @Value >= 301)

    Begin

    print 'Maximum'

    End

    else

    Begin

    print 'Not Found'

    End

     

     



    Taha Zubair Ahmed Software Emgineer.
    Tuesday, September 21, 2010 8:13 PM
  • You may use some thing like that

     var temp = from v in TotalBookingPrice
                where v.ID > 94685
                select v.ID;
          int value = temp.FirstOrDefault();
          string toPrint =string.Empty;
          if (value <= 100)
            toPrint = "Blow";
          if(value <= 300) 
            toPrint = "Normal";
          if(value >= 301) 
            toPrint = "Maximum";
          else
            toPrint = "Not Found";
    
    Wednesday, September 22, 2010 7:12 AM
  • Thank u for ur Reply i will reply soon....
    Taha Zubair Ahmed Software Emgineer.
    Wednesday, September 22, 2010 7:20 PM
  • welcome
    Wednesday, September 22, 2010 7:41 PM
  •  int
     value = temp.FirstOrDefault();
    

    This Line create Errors to build i'm trying to solve it

     


    Taha Zubair Ahmed Software Emgineer.
    Thursday, September 23, 2010 7:14 PM
  • int value = (from p in dc.Patients
                            select p.PAge.Value).FirstOrDefault();

     

    use Value Keyword to solve this problems.......

     


    Taha Zubair Ahmed Software Emgineer.
    Friday, September 24, 2010 6:49 PM
  • use another name for value
    Saturday, September 25, 2010 9:29 AM
  • int result= (from p in dc.Patients
                            select p.PAge.Value).FirstOrDefault();

     Now its ok ....

    use Value Keyword to solve this problems.......


    Taha Zubair Ahmed Software Emgineer.
    • Marked as answer by Larcolais Gong Tuesday, September 28, 2010 5:20 AM
    Saturday, September 25, 2010 12:47 PM