2010-12-10 142 views
0

我有这个奇怪的问题。我有一个简单的搜索要求,用户可以根据几个搜索标准搜索给定的客户(说客户)。用户可以选择使用或不使用标准。搜索条件需要“与”所有条件。所以我写这样的代码(工作)where子句中的多个语句

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子句的数量?

感谢

+0

错误是什么? – 2010-12-10 13:55:54

回答

1

为了简化生成的查询,你可以使用:

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条款时,有一个需要它。