Is there a better to build Dynamic LINQ query RRS feed

  • Question

  • i have question about IQueryable i am using EF with oracle DB

    i want to filter my data acording to user input

    if the one of the input is wrong or both input are empty the query should not return any thing

    this was my first code


     Entities ctx = new Entities();
             var OraLINQ2 =
                 (from dev in ctx.OES_DEVICE
                  where dev.DEV_SER == TBOX_MODULE.Text
                  && dev.DEV_MODEL_NAME == TBOX_SER.Text
                  orderby dev.DEV_INV_NO
                  select dev);

    so i try to use IQueryable insted

    this is the IQueryable code

     IQueryable<OES_DEVICE> OraLINQ2 = ctx.OES_DEVICE;
                     if (!string.IsNullOrWhiteSpace(TBOX_MODULE.Text))
                          OraLINQ2 = OraLINQ2.Where(dev => dev.DEV_MODEL_NAME == TBOX_MODULE.Text);
                   if (!string.IsNullOrWhiteSpace(TBOX_SER.Text))
                         OraLINQ2 = OraLINQ2.Where(dev => dev.DEV_SER == TBOX_SER.Text);

    but if both the TBOX_MODULE.Text and TBOX_SER.Text are null

    the query will return all the data from the table

    is there a better way to do that ??

    Thursday, March 20, 2014 10:25 AM


  • Hello MSD,

    In actually, both of your two query will return the IQueryable<T> type. You can use the GetType() method to check the type of the OraLINQ2 in your first query.

    IQueryable means that it will translate the LINQ query to TSQL and then pass the TSQL to database and execute it without loading data to local firstly.

    So I think your first query already meets your requirement.

    If you want your query more flexible, I would like to recommend the LINQ Dynamic Query Library:


    Hope this helpful.


    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.

    Friday, March 21, 2014 3:11 AM