dynamic Linq queries with Entity Framework
I am aware of few efforts in constructing Linq que开发者_JS百科ries 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..
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?
精彩评论