2010-12-22 38 views
2

我有一個LINQ到SQL查詢是這樣的:使用List作爲LINQ to SQL查詢的條件

String NameCondition = "Test"; 
String EmailCondition = "[email protected]"; 

IQueryable<User> Users = Repository.Where(x => x.Name.Equals(NameCondition) && x.Email.Equals(EmailCondition)); 

但現在我有一個條件列表,我想拉用戶從如果它們匹配其中的任何一個(如SQL中的OR語句)。所以我有一個字典列表,以及每個字典與姓名和電子郵件的條件:

List<Dictionary<String, String>> Conditions; 

我怎麼能執行一個LINQ到使用這些條件的SQL查詢?

+1

爲什麼你有一個詞典的列表?這聽起來像是你說每個字典只包含一個條目。是這樣嗎? – 2010-12-22 15:18:30

回答

0

使用PredicateBuilder約瑟夫阿爾巴哈利

http://www.albahari.com/nutshell/predicatebuilder.aspx

下面是一個示例使用。

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); 
} 
+2

爲什麼編輯丹尼爾。我認爲用法示例支持鏈接很好 – 2010-12-22 15:22:06

0

而是詞典的使用在.net 3.5 List<KeyValuePair<string, string>>List<Tuple<string, string>>在.NET 4.0中。

看到某種相似question