简体   繁体   English

LINQ 到 SQL Where 子句可选标准

[英]LINQ to SQL Where Clause Optional Criteria

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.我正在使用 LINQ 到 SQL 查询,遇到了一个问题,我有 4 个可选字段来过滤数据结果。 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...具体来说,一些可能有一个值或有一个空字符串的文本框和一些可能有一个值被选择或可能没有的下拉列表......

For example:例如:

    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. Product Number - Comes from another table that can be joined to TagsHeaders.产品编号 - 来自另一个可以连接到 TagsHeaders 的表。
  2. PO Number - a field within the TagsHeaders table. PO 编号 - TagsHeaders 表中的一个字段。
  3. Order Number - Similar to PO #, just different column.订单号 - 类似于 PO #,只是不同的列。
  4. Product Status - If the user selected this from a drop down, need to apply selected value here.产品状态 - 如果用户从下拉列表中选择此项,则需要在此处应用所选值。

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!我已经有的查询很好用,但是要完成 function,需要能够在 where 子句中添加这 4 个其他项目,只是不知道如何!

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.然后根据一个条件,添加额外的 where 约束。

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

I am not sure how to code this with the query syntax but id does work with a lambda.我不确定如何使用查询语法对此进行编码,但 id 确实适用于 lambda。 Also works with query syntax for the initial query and a lambda for the secondary filter.也适用于初始查询的查询语法和二级过滤器的 lambda。

You can also include an extension method (below) that I coded up a while back to include conditional where statements.您还可以包含我不久前编写的扩展方法(如下),以包含条件 where 语句。 (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);

The extension method:扩展方法:

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;
}

Here is the same extension method for IEnumerables:这是 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;
}

Just need to use a conditional checking for the parameter's existence.只需要使用条件检查参数的存在。 For instance:例如:

where (string.IsNullOrEmpty(ProductNumber) || ProductNumber == tags.productNumber)

That way if the product number isn't entered that expression will return true in all cases, but if it is entered it will only return true when matching.这样,如果没有输入产品编号,该表达式将在所有情况下都返回 true,但如果输入了它,它只会在匹配时返回 true。

You have the ability to OR with ||.您可以使用 || 进行 OR。

Check out this thread, as it might give you some nice pointers: C# LINQ equivalent of a somewhat complex SQL query看看这个线程,因为它可能会给你一些很好的指示: C# LINQ 相当于一个有点复杂的 SQL 查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM