问题描述
我正在使用 LINQ to 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.
- 产品编号 - 来自可以连接到 TagsHeaders 的另一个表.
- 采购订单编号 - TagsHeaders 表中的一个字段.
- 订单号 - 与采购订单编号类似,只是不同的列.
- 产品状态 - 如果用户从下拉列表中选择了此项,则需要在此处应用所选值.
我已有的查询很好用,但要完成该功能,需要能够在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!
推荐答案
您可以对原始查询进行编码:
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;
然后根据条件,添加额外的 where 约束.
And then based on a condition, add additional where constraints.
if(condition)
query = query.Where(i => i.PONumber == "ABC");
我不确定如何使用查询语法对此进行编码,但 id 确实适用于 lambda.也适用于初始查询的查询语法和二级过滤器的 lambda.
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.
您还可以包含我不久前编写的扩展方法(如下)以包含条件 where 语句.(不适用于查询语法):
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 to SQL Where 子句可选条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!