[英]Using a List as a condition for LINQ to SQL query
I have a LINQ to SQL query that goes like this: 我有一个LINQ to SQL查询,如下所示:
String NameCondition = "Test";
String EmailCondition = "test@test.com";
IQueryable<User> Users = Repository.Where(x => x.Name.Equals(NameCondition) && x.Email.Equals(EmailCondition));
But now I have a list of conditions where I would like to pull Users from if they match any of them (like an OR statement in SQL). 但是现在我有了一个条件列表,如果用户匹配它们,我想从中拉出条件(例如SQL中的OR语句)。 So I have a list of dictionaries, and each dictionary is a condition with a name and email: 所以我有一个字典列表,每个字典都是一个带有名称和电子邮件的条件:
List<Dictionary<String, String>> Conditions;
How can I perform a LINQ to SQL query using those conditions? 如何使用这些条件执行LINQ to SQL查询?
Use PredicateBuilder
by Josef Albahari 使用Josef Albahari的PredicateBuilder
http://www.albahari.com/nutshell/predicatebuilder.aspx http://www.albahari.com/nutshell/predicatebuilder.aspx
Here is a sample usage. 这是一个示例用法。
IQueryable<Product> SearchProducts (params string[] keywords)
{
var predicate = PredicateBuilder.False<Product>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
return dataContext.Products.Where (predicate);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.