I am using EF Code first 4.2,
What sort of solution do you propose when the where clause needs to be dynamically built?
Yet Include functionality would be highly required:
var results = db.Set<dynamicType>.Where("dynamic conditions").Include("....");
The dynamic condition above needs to lookup to another table to filter the records:
If I wanted to write that in Linq expression it would be something like:
var result = db.Set<Contact>().Where(c=>c.AccountId == _Id_param || db.Set<LinkTable>().Any(a=>a.FkFieldId == c.AccountId && a.ParentId == _Id_param)).Include("Quotes");
I basically needs the dynamic linq of the above expression, since for different types the Where clause fields changes (Contact is only an example), for example in one Model the FK field may be “AccountId” and in another it needs to be “AccountFKId”. So the Where clause has to be dynamic!
UPDATE
I was able to solve the issue with directly modifying the expression tree.
Using an idea from TomasP’s blog helped a lot:
The key was to create a second IQueryable for the internal query and then pass it as an expression to the existing dynamic model’s IQueryable expression.
Now you can pass the anyMethodExpr to the original Entity’s IQueryable where clause.