我已经了解了如何使用linq to sql进行条件查询,还了解了如何使用OR where子句。不幸的是,我想不出如何同时做这两件事。我可以做一个条件where子句,如下所示:
var ResultsFromProfiles = from AllPeeps in SearchDC.aspnet_Users
select AllPeeps;
if (SearchFirstNameBox.Checked)
{
ResultsFromProfiles = ResultsFromProfiles.Where(p => p.tblUserProfile.FirstName.Contains(SearchTerm));
}
if (SearchLastNameBox.Checked)
{
ResultsFromProfiles = ResultsFromProfiles.Where(p => p.tblUserProfile.LastName.Contains(SearchTerm));
}这将使我得到名字和姓氏包含搜索词的所有配置文件。
或者我可以这样做:
var ResultsFromProfiles = from p in SearchDC.aspnet_Users
where p.tblUserProfile.LastName.Contains(SearchTerm) ||
p.tblUserProfile.FirstName.Contains(SearchTerm)
select p;这将使我获得名字或姓氏包含搜索词的任何配置文件。
我有一堆复选框,用户可以在其中指定他们想要搜索搜索词的字段,所以我希望能够构建一个查询,该查询将像上面的第一个代码片段中那样有条件地添加它们,但将它们作为OR添加,以便它们像第二个代码片段一样工作。这样,它将在指定字段中的任何位置搜索任何匹配项。
有什么建议吗?
发布于 2009-12-17 10:10:47
是的,使用PredicateBuilder。它允许您使用And或or语义构建动态查询。它是免费和稳定的--我到处都在用它。
发布于 2009-12-17 10:48:20
为此,一种方法是操作查询的LINQ表达式树。在本例中,您需要构建一个lambda表达式,并将其替换为对Where的调用。下面是一个基于列表的工作示例,但是操作表达式树的代码是相同的,而与查询提供程序无关。
List<User> Users = new List<User>();
Users.Add(new User() { FirstName = "John", LastName = "Smith" });
Users.Add(new User() { FirstName = "Jane", LastName = "Smith" });
string Query = "John";
var Queryable = Users.AsQueryable();
var Results = (from u in Queryable
select u);
//initial method call... the lambda u => false is a place-holder that is about to be replaced
MethodCallExpression WhereExpression = (MethodCallExpression)Results.Where(u => false).Expression;
//define search options
Expression<Func<User, string, bool>> FilterLastName = (u, query) => u.LastName.Contains(query);
Expression<Func<User, string, bool>> FilterFirstName = (u, query) => u.FirstName.Contains(query);
//build a lambda based on selected search options... tie the u parameter to UserParameter and the query parameter to our Query constant
ParameterExpression UserParameter = Expression.Parameter(typeof(User), "u");
Expression Predicate = Expression.Constant(false); //for simplicity, since we're or-ing, we'll start off with false || ...
//if (condition for filtering by last name)
{
Predicate = Expression.Or(Predicate, Expression.Invoke(FilterLastName, UserParameter, Expression.Constant(Query)));
}
//if (condition for filtering by first name)
{
Predicate = Expression.Or(Predicate, Expression.Invoke(FilterFirstName, UserParameter, Expression.Constant(Query)));
}
//final method call... lambda u => false is the second parameter, and is replaced with a new lambda based on the predicate we just constructed
WhereExpression = Expression.Call(WhereExpression.Object, WhereExpression.Method, WhereExpression.Arguments[0], Expression.Lambda(Predicate, UserParameter));
//get a new IQueryable for our new expression
Results = Results.Provider.CreateQuery<User>(WhereExpression);
//enumerate results as usual
foreach (User u in Results)
{
Console.WriteLine("{0} {1}", u.FirstName, u.LastName);
}Working with expression trees通常可以通过使用访问者模式来简化,但我省略了这一点,以便您可以更清楚地看到必须完成的工作。
发布于 2009-12-17 10:17:03
这里有一个建议。
我还没有尝试过编译和运行它,而且LinqToSQL充满了惊喜,所以不能保证:-)
var ResultsFromProfiles = from AllPeeps in SearchDC.aspnet_Users select AllPeeps;
IEnumerable<AspNet_User> total = new AspNew_User[0];
if (SearchFirstNameBox.Checked)
{
total = total.Concat(ResultsFromProfiles.Where(p => p.tblUserProfile.FirstName.Contains(SearchTerm));}
}
if (SearchLastNameBox.Checked)
{
total = total.Concat(ResultsFromProfiles.Where(p => p.tblUserProfile.LastName.Contains(SearchTerm));
}
total = total.Distinct();https://stackoverflow.com/questions/1919065
复制相似问题