首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于过滤linq的多个选项

用于过滤linq的多个选项
EN

Stack Overflow用户
提问于 2012-09-26 16:26:21
回答 4查看 2.8K关注 0票数 0

我想有3-4个搜索选项。现在我在If语句中使用我的查询。

代码语言:javascript
复制
     if inputcol1 > 0 And Not inputCol2 = "" then 
         Dim list = (From P In db.table
                     Where P.column1 = inputCol1 and P.column2 = inputCol2
                     Select P).ToList()
     end if

和一个(或4个)多个条件,但使用几乎相同的查询,只有一个不同的where子句。因此,这个查询是简单而简短的。因此,如果我有一个非常大的查询,它将是一个很大的混乱。我不希望其他程序员为了几个选项而阅读这么多代码。

有没有一种简单的方法来查看是否填写了搜索选项并对其进行查询?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-09-26 19:45:58

最好的方法是有条件地展开查询:

代码语言:javascript
复制
Dim query = (From P In db.table Select P)

If inputCol1.HasValue
    query = (From P in query Where P.column1 = inputCol1 Select P)
End If
If inputCol2.HasValue
    query = (From P in query Where P.inputCol2 = inputCol2 Select P)
End If
' And so on...

Dim list = query.ToList()

使用Not inputCol2.HasValue OrElse P.Value = inputCol2的条件过滤将创建一个带有无用谓词的查询。通过有条件地扩展您的查询,只有重要的谓词将被合并。

票数 0
EN

Stack Overflow用户

发布于 2018-12-14 04:35:24

现在已经很晚了,但我目前正在做一个VB.NET项目,除了Gert Arnold的answer之外,在selection e中拥有相同的对象也很重要。例如,如果假设P是项目实体,并且在最终结果(过滤之后)中选择了额外的列或对象详细信息(如query = (From P in query Select P.Name, P.Deadline) ),则会抛出强制转换异常,因为变量querySelect P.Name, P.Deadline的结构或对象不同。因此,如果您希望在最终选择中具有不同的结构,请使用另一个变量,例如:

代码语言:javascript
复制
Dim query = (From P in query Where P.Col2 = inputCol2 Select P)
Dim result = From P In query Select P.Name, P.Deadline ... 
'and then displaying it 
DataGridView.DataSource = result.toList()

然而,我确实尝试过用result = From P in query Select New With {P.Name, P.Deadline}创建一个新类型,但它不起作用。我不知道为什么,如果我有空闲时间,我会深入研究的。

票数 1
EN

Stack Overflow用户

发布于 2012-09-26 17:15:16

您可能会喜欢尝试LinqKit。有了这个库,您就有了一个带有方法的PredicateBuilder类:

代码语言:javascript
复制
public static Expression<Func<T, bool>> True<T>();
public static Expression<Func<T, bool>> False<T>();
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2);
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2);

这些是Expression对象的扩展,可以很容易地从lambda创建。

使用这样的表达式,您可以执行yourDataSource.Where(expression)

很抱歉使用c#符号,我不认识VB.net...如果有人想把它修复到VB上,请随意。

编辑:

嗯,PredicateBuilder只是一种简洁的语法糖。在他们的网站上,你可以找到完整的源代码,非常简单。不幸的是,在C#中。它是这样的:

代码语言:javascript
复制
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

public static class PredicateBuilder
{
  public static Expression<Func<T, bool>> True<T> ()  { return f => true;  }
  public static Expression<Func<T, bool>> False<T> () { return f => false; }

  public static Expression<Func<T, bool>> Or<T> (this Expression<Func<T, bool>> expr1,
                                                      Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  }

  public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
                                                       Expression<Func<T, bool>> expr2)
  {
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
    return Expression.Lambda<Func<T, bool>>
          (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
  }
}

就是这样!表达式(在.net中是标准的,不需要额外的库)提供了一些很好的方法来处理它们,并且可以在where子句中使用它们。试一试:)

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

https://stackoverflow.com/questions/12597542

复制
相关文章

相似问题

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