我想有3-4个搜索选项。现在,我在 If 语句中使用我的查询。
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 子句。因此,此查询是简化且简短的。因此,如果我有一个非常大的查询,那将是一个大混乱。 而且我不希望其他程序员只为几个选项而阅读这么多代码。
有没有一种适当的简单方法来查看搜索选项是否已填写并查询它?
现在已经很晚了,但我目前正在做一个 VB.NET 项目,除了 Gert Arnold 的答案之外,在选择 i-e 中使用相同的对象也很重要。链接时 P,因为例如,如果说 P 是项目实体,并且在最终结果(过滤后)您正在选择其他列或对象详细信息,例如 query = (From P in query Select P.Name, P.Deadline)
,它将抛出强制转换异常,因为变量 query
与 Select P.Name, P.Deadline
的结构或对象不同。因此,如果您希望在最终选择中使用不同的结构,请使用另一个变量,例如:
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}
创建一个新类型,但它不起作用。我不知道为什么,如果我有空闲时间离开,我会深入研究它。
最好的方法是有条件地扩展查询:
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
进行条件筛选将创建包含无用谓词的查询。通过有条件地扩展查询,将仅合并重要的谓词。
你的代码走在正确的轨道上......你只需要更改三元运算符的 if 语句:
Dim table = New Dictionary(Of Integer, String)
table.Add(1, "one")
table.Add(2, "two")
table.Add(3, "three")
Dim inputCol1 As Integer
Dim inputCol2 As String = "one"
Dim list = (From P In table Where _
(inputCol1 < 1 OrElse P.Key = inputCol1) _
And (inputCol2 = "" OrElse P.Value = inputCol2) _
Select P).ToList()
有关此问题的有关此情况的详细信息:条件筛选
你可能喜欢尝试 LinqKit。使用此库,您将拥有一个包含方法的PredicateBuilder
类:
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# 中。事情是这样的:
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
子句中使用。试一试:)