首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >where子句中的多个语句

问where子句中的多个语句
EN

Stack Overflow用户
提问于 2010-12-10 20:53:46
回答 1查看 823关注 0票数 0

我有个奇怪的问题。我有一个简单的搜索需求,用户可以基于几个搜索标准搜索给定的实体(比如客户)。用户可以选择是否使用条件。搜索条件需要'AND‘所有条件。所以我写了这样的代码(它是有效的)

IQueryable _customer;

代码语言:javascript
复制
        _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子句数量的设置?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2010-12-10 21:00:54

要简化生成的查询,您可以使用:

代码语言:javascript
复制
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子句。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4408988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档