views:

19

answers:

1

I am aware of few efforts in constructing Linq queries dynamically, such as this, and this.

None seem to be ideal as I would like to avoid putting expressions in a string, and omitting a where if it is not needed.

My main concern is that the query is optimized for the database, and dynamically omits unnecessary clauses whenever possible.

Are there any new developments in EF 4.0 for such scenarios?

UPDATE

here is one link i found very helpful: http://www.albahari.com/nutshell/predicatebuilder.aspx indeed, adding "And" filters dynamically is trivial, and adding "Or" filters can be done easily using predicate builder:

var predicate = PredicateBuilder.False<Product>();
predicate = predicate.Or (p => p.Description.Contains (temp));

and according to LinqPad the sql gets emitted accordingly to what filters were applied..

A: 

For omitting the Where cause (pseudocode, hope I understood your question correctly):

var query = IQueryable<Foo>();

if(someCondition)
    query = query.Where(......);

var result = query.Select(.......);

For dynamic queries - I haven't heard about anything new. IMHO we will have to stay with strings. Can you come up with some better approach?

Jakub Konecki