簡體   English   中英

使用列表作為LINQ to SQL查詢的條件

[英]Using a List as a condition for LINQ to SQL query

我有一個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));

但是現在我有了一個條件列表,如果用戶匹配它們,我想從中拉出條件(例如SQL中的OR語句)。 所以我有一個字典列表,每個字典都是一個帶有名稱和電子郵件的條件:

List<Dictionary<String, String>> Conditions;

如何使用這些條件執行LINQ to SQL查詢?

使用Josef Albahari的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);
}

在.NET 3.5中使用List<KeyValuePair<string, string>>而不是字典List<KeyValuePair<string, string>>在.NET 4.0中使用List<Tuple<string, string>>

看到類似的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM