Entity framework where子句中的多个语句
我有一个奇怪的问题。我有一个简单的搜索要求,用户可以根据几个搜索标准搜索给定的实体(比如说客户)。用户可以选择是否使用标准。搜索条件需要“和”所有条件。所以我写了这样的代码(这很有效) IQueryable\u客户Entity framework where子句中的多个语句,entity-framework,Entity Framework,我有一个奇怪的问题。我有一个简单的搜索要求,用户可以根据几个搜索标准搜索给定的实体(比如说客户)。用户可以选择是否使用标准。搜索条件需要“和”所有条件。所以我写了这样的代码(这很有效) IQueryable\u客户 _customer = from c in DS.properties where (txtCustomerName.Text.Length == 0 || c
_customer = from c in DS.properties
where
(txtCustomerName.Text.Length == 0 || c.name == txtCustomerName.Text)
&& (txtpropcust1.Text.Length == 0 || c.customfield1 == txtpropcust1.Text)
&& (txtpropcust2.Text.Length == 0 || c.customfield2 == txtpropcust2.Text)
&& (txtpropcust3.Text.Length == 0 || c.customfield3 == txtpropcust3.Text)
&& (txtpropcust4.Text.Length == 0 || c.customfield4 == txtpropcust4.Text)
&& (txtpropcust13.Text.Length == 0 || c.customfield13 == txtpropcust13.Text)
select c;
GridView1.DataContext = _customer;
问题是,如果我有14个where子句,EF会抛出一个错误-13起作用-14不起作用
我正在WPF应用程序中使用EF+WCF数据服务。是否存在限制where子句数量的设置
感谢为了简化结果查询,您可以使用:
var customers = DS.properties;
if (txtCustomerName.Text.Length > 0)
customers = customers.Where(x => x.name == txtCustomerName.Text);
if (txtpropcust1.Text.Length > 0)
customers = customers.Where(x => x.customfield1 == txtpropcust1.Text);
// etc
_customer = customers;
GridView1.DataContext = _customer;
注意,这只会在需要时添加SQLwhere
子句