构建动态 where 子句,Linq To Sql
我使用的是 EF Code First 4.2, 当需要动态构建 where 子句时,您提出什么样的解决方案? 然而,包含功能是非常需要的:
var results = db.Set<dynamicType>.Where("dynamic conditions").Include("....");
上面的动态条件需要查找另一个表来过滤记录: 如果我想在 Linq 表达式中编写它,它会是这样的:
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");
我基本上需要上述表达式的动态 linq,因为对于不同类型,Where 子句字段会发生变化(Contact 只是一个示例),例如在一个模型中 FK字段可能是“AccountId”,而在另一个字段中则需要是“AccountFKId”。所以Where子句必须是动态的!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IQueryable
是可组合的,因此您可以即时构建查询:Linq 是强类型的,因此您始终至少必须知道要在
Set<>调用。您可以使其通用但不是动态的(除非您要通过反射完整地编写它)。
您可以使用 动态 linq 来定义带有字符串的 where 条件,但同样,您至少必须知道
Set>>
的类型。IQueryable
is composable so you can build query on the fly:Linq is strongly typed so you always at least have to know what type are you going to start with in your
Set<>
call. You can make it generic but not dynamic (unless you are going to write it whole through reflection).You can use dynamic linq to define where conditions with strings but again you will at least have to know type for
Set<>
.更新
我能够通过直接修改表达式树来解决问题。
使用 TomasP 博客 的想法有很大帮助:
关键是创建第二个 IQueryable内部查询,然后将其作为表达式传递给现有动态模型的 IQueryable 表达式。
现在您可以将anyMethodExpr 传递到原始实体的IQueryable where 子句。
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.