Dynamic query in entity framework RRS feed

  • Question

  • I have to fetch data from a table based on values into the columns of the table. The problem is, name and number of columns for my filter criteria keeps on changing.

    How can I write dynamic query in Entity framework, so that I could specify only the desired columns in the query. Example:
    1) select * from XXX where yy ='abc' and zz = 'mno'

    Next time I may want to select as:
    Select * from XXX where yy='abc' and aa = 'efg'

    Note: columns 'aa' and 'zz' are different.
    Wednesday, March 18, 2009 1:53 PM

All replies

  • Hi,
    You can use ESQL and write your queries as a string


    Wednesday, March 18, 2009 2:03 PM
  •  Is there any way to write parameterized query in the above mentioned case??

    I have read "CompiledQuery" does allow us to write parameterized query. Is there any way to define the actual query as "String" in "CompiledQuery"?
    Thursday, March 19, 2009 4:20 AM
  • If you are always combining filters using &&, you could do something like this in Linq To Entities:

    IQueryable<XXX> query = context.XXX;

    if (filter on aa)
       query = query.Where(x => x.aa == 'efg');
    if (filter on yy)
       query = query.Where(x => x.yy == 'abc');
    if (filter on zz)
        query = query.Where(x => x.zz == 'mno');

    Hope this helps

    Thursday, March 19, 2009 9:05 AM