我有个奇怪的问题。我有一个简单的搜索需求,用户可以基于几个搜索标准搜索给定的实体(比如客户)。用户可以选择是否使用条件。搜索条件需要'AND‘所有条件。所以我写了这样的代码(它是有效的)
IQueryable _customer;
_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子句数量的设置?
谢谢
发布于 2010-12-10 21:00:54
要简化生成的查询,您可以使用:
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;请注意,这将仅在需要时添加SQL where子句。
https://stackoverflow.com/questions/4408988
复制相似问题