【发布时间】:2012-02-25 16:35:30
【问题描述】:
我正在使用 linq 到实体 (EF)。 我有一个带有 4 个字符串参数的构造函数。根据什么参数不为空,我必须构建 linq 查询。我可以使用 if else 语句,但我还有其他带有 10 个参数的构造函数,在这种情况下将有很多组合需要检查。
例子:
Constructor(p1,p2,p3,p4)
{
var prod= from p in ctxt.products.expand("items\details")
where p.x==p1 && p.xx==p2 && p.xxx==p3 && p.xxxx==p4
select p;
}
在上面的 where 子句中,只有当参数不为空时才应该进行条件检查。 IE。, 如果 p2 为空,那么 where 子句应该是这样的
where p.x==p1 && p.xxx==p3 && p.xxxx==p4
如果 p2 和 p3 为空,则
where p.x==p1 && p.xxxx==p4
谁能告诉我如何处理这个问题。如果可能的话,您可以为此提供示例代码
【问题讨论】:
标签: linq entity-framework dynamic where-clause