none
How to use if condition in select with Linq RRS feed

  • Question

  • Hi everybody,

    I do not know if that's possible, but is there any way to build a query in Linq using "if" if a parameter is not completed or does not meet a condition, as shown below:

       public DataTable getProduct(string description,int group)

    {

    StringBuilder sb = new StringBuilder();

    sb.Append("Select * from Product ");

    sb.Append("Where Description =" + description.ToString());

    if

    (group > 0)

    {

       sb.Append(" and group = " + group.ToString());

    }

    return recover(sb.ToString()); //My Method 

    

    }

     


    Duan Brito - Analista de Sistemas/Programador
    • Edited by Duan Brito Saturday, October 22, 2011 12:15 PM
    Saturday, October 22, 2011 12:13 PM

Answers

All replies

  • Hi,

     

    if you want to create query linq dinamically you can use Expression Tree.

     

    http://msdn.microsoft.com/en-us/library/bb397951.aspx

     

    Gab

    Monday, October 24, 2011 10:35 AM
  • Hi,

    Plase, take a look at this other thread:

    http://stackoverflow.com/questions/11194/conditional-linq-queries

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Monday, October 24, 2011 11:35 AM
  • JA Reyes,

    Unfortunately it is not a solution to my problem as I understand it loads the entire contents of atable and then where does that content, if I do that to a table with more than 50 000 records had been a very slow query.

    Thanks for the help.


    Duan Brito - Analista de Sistemas/Programador
    Wednesday, October 26, 2011 1:59 PM
  • Gabrog,

    WOW, I thought there was an easier way to do this.

    Thanks for the help.


    Duan Brito - Analista de Sistemas/Programador
    Wednesday, October 26, 2011 2:08 PM
  • Hi again,

    Results are not returned until ToList() method is performed, so you can add as much conditions after as you want.

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Wednesday, October 26, 2011 2:32 PM
  • Just put everything into the where

     

    from row in Table where group == 0 or row.group==group select row;

     

    I would actually do the same in sql instead of concatinating strings

    select * from Table where @group=0 or @group=group

     

     

    In example 1 the group==0 is evaluated very early, and the sql for the clause is not generated if true.

    In example 2, sql server just evaluates it left to right, and therefore do it as fast as the string concat.

    Friday, October 28, 2011 12:25 PM
  • Well,

    Try with ExecuteQuery method then:

    http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executequery.aspx

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Friday, October 28, 2011 12:41 PM