开发者

Multiple statements in where clause

开发者 https://www.devze.com 2023-01-29 17:00 出处:网络
I have this strange problem. I have a simple search requirements where a user can search a given entitiy (Say customer) based on several search criterias. User may choose to use a criteria or not. The

I have this strange problem. I have a simple search requirements where a user can search a given entitiy (Say customer) based on several search criterias. User may choose to use a criteria or not. The search conditions need to 'AND' all the criteria. So I write code like this (which works)

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;

      开发者_Go百科  GridView1.DataContext = _customer;        

The problem is that if I have 14 where clauses, the EF throws an error- 13 works - 14 does not.

I am using EF+WCF data service in a WPF application. Is there a setting somewhere which limits the number of where clauses?

Thanks


To simplify the resulting query, you could use:

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;

Note that this will only add SQL where clauses when there's a need for it.

0

精彩评论

暂无评论...
验证码 换一张
取 消