LINQ到SQL Where子句可选标准 [英] LINQ to SQL Where Clause Optional Criteria

查看:101
本文介绍了LINQ到SQL Where子句可选标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用LINQ合作,SQL查询和所遇到的问题,我有4个可选字段来过滤数据结果上。通过可选的,我的意思是有选择输入值与否。具体而言,这可能有一个值或有一个空串和几个下拉列表,可以有选择的值或几个文本框也许不是...

I am working with a LINQ to SQL query and have run into an issue where I have 4 optional fields to filter the data result on. By optional, I mean has the choice to enter a value or not. Specifically, a few text boxes that could have a value or have an empty string and a few drop down lists that could have had a value selected or maybe not...

例如:

    using (TagsModelDataContext db = new TagsModelDataContext())
     {
        var query = from tags in db.TagsHeaders
                    where tags.CST.Equals(this.SelectedCust.CustCode.ToUpper()) 
                    && Utility.GetDate(DateTime.Parse(this.txtOrderDateFrom.Text)) <= tags.ORDDTE
                    && Utility.GetDate(DateTime.Parse(this.txtOrderDateTo.Text)) >= tags.ORDDTE
                    select tags;
        this.Results = query.ToADOTable(rec => new object[] { query });
    }

现在我需要添加以下字段/过滤器,但只有当他们是由用户提供。

Now I need to add the following fields/filters, but only if they are supplied by the user.


  1. 产品数量 - 从可以加入到TagsHeaders另一个表配

  2. PO数量 - 在TagsHeaders表中的一个字段

  3. 订单号 - 以PO#相似,只是不同的列

  4. 产品状态 - 如果用户选择了这个从下拉,需要在这里申请选定值

查询我已经是伟大的工作,但完成的功能,需要能够在where子句中添加这4个等物品,只是不知道怎么办!

The query I already have is working great, but to complete the function, need to be able to add these 4 other items in the where clause, just don't know how!

推荐答案

您可以code原始查询:

You can code your original query:

var query = from tags in db.TagsHeaders
                where tags.CST.Equals(this.SelectedCust.CustCode.ToUpper()) 
                && Utility.GetDate(DateTime.Parse(this.txtOrderDateFrom.Text)) <= tags.ORDDTE
                && Utility.GetDate(DateTime.Parse(this.txtOrderDateTo.Text)) >= tags.ORDDTE
                select tags;

然后根据条件,增加额外的限制在哪里。

And then based on a condition, add additional where constraints.

if(condition)
    query = query.Where(i => i.PONumber == "ABC");

我不知道如何code这与查询语法,但ID不与拉姆达的工作。还与初始查询和二级过滤拉姆达查询语法工作。

I am not sure how to code this with the query syntax but id does work with a lambda. Also works with query syntax for the initial query and a lambda for the secondary filter.

您还可以包括扩展方法(下),我codeD了,而回包括条件,语句。 (不与查询语法很好地工作):

You can also include an extension method (below) that I coded up a while back to include conditional where statements. (Doesn't work well with the query syntax):

        var query = db.TagsHeaders
            .Where(tags => tags.CST.Equals(this.SelectedCust.CustCode.ToUpper()))
            .Where(tags => Utility.GetDate(DateTime.Parse(this.txtOrderDateFrom.Text)) <= tags.ORDDTE)
            .Where(tags => Utility.GetDate(DateTime.Parse(this.txtOrderDateTo.Text)) >= tags.ORDDTE)
            .WhereIf(condition1, tags => tags.PONumber == "ABC")
            .WhereIf(condition2, tags => tags.XYZ > 123);

扩展方法:

public static IQueryable<TSource> WhereIf<TSource>(
    this IQueryable<TSource> source, bool condition,
    Expression<Func<TSource, bool>> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}

下面是IEnumerables一样扩展方法:

Here is the same extension method for IEnumerables:

public static IEnumerable<TSource> WhereIf<TSource>(
    this IEnumerable<TSource> source, bool condition,
    Func<TSource, bool> predicate)
{
    if (condition)
        return source.Where(predicate);
    else
        return source;
}

这篇关于LINQ到SQL Where子句可选标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆