2010-12-10 144 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條款時,有一個需要它。