简体   繁体   English

动态LINQ to Entities,如何基于变量构建查询

[英]Dynamic LINQ to Entities, how to build query based on variables

The query I need to build is this: 我需要建立的查询是这样的:

query = query.Where(s => 
           (
              (s.Title.Contains(title1) && s.EpisodeTitle.Contains(episodeTitle1))
               ||
              (s.Title.Contains(title2) && s.EpisodeTitle.Contains(episodeTitle2)))
            );

The only issue is, s.Title and s.EpisodeTitle are dynamic. 唯一的问题是s.Title和s.EpisodeTitle是动态的。

Meaning that the following variables could be part of the query: 这意味着以下变量可能是查询的一部分:

(string title1 = null,
  string title2 = null,
  string episodeTitle1 = null,
  string episodeTitle2 = null,
  string genre = null,
  string directorName = null,
  string releaseYear = null,
  string seasonEpisode = null,
  string showTypeDescription = null)

eg 例如

query = query.Where(s => 
           (
              (s.DirectorName.Contains(directorName) && s.ShowTypeDescription.Contains(ShowTypeDescription))
               ||
              (s.releaseYear.Contains(releaseYear) && s.genre.Contains(genre)))
            );

In ANY type of combination. 在任何类型的组合中。

How can I construct this query without having to take into account EVERY SINGLE possibility here? 如何构造此查询而不必考虑这里的每一种可能性?

If you only need AND logic you could just call .Where() repeatedly for every attribute that requires searching on. 如果只需要AND逻辑,则可以针对需要搜索的每个属性重复调用.Where()

if(title != null) query = query.Where(x=>x.Title == title);
if(genre != null) query = query.Where(x=>x.Genre == genre);

If your query is always of a certain structure and you want to ignore null search values you could do one big query but short circuit the attribute comparison with null checks. 如果您的查询始终具有特定的结构,并且您想忽略空搜索值,则可以执行一个大查询,但可以使用空检查来缩短属性比较。

query = query.Where(s => 
  (
    ((title1 == null || s.Title.Contains(title1)) 
        && (episodeTitle1 == null || s.EpisodeTitle.Contains(episodeTitle1))
     ||
    ((title2 == null || s.Title.Contains(title2)) 
       && (episodeTitle2 == null || s.EpisodeTitle.Contains(episodeTitle2))))
        );

However if you need full control over the query then you will need to look at using PredicateBuilder or System.Linq.Expressions to build a specific query to search on the necessary attributes. 但是,如果您需要对查询的完全控制,则需要使用PredicateBuilder或System.Linq.Expressions构建特定的查询以搜索必要的属性。 Here is a useful tutorial on Linq.Expressions - http://msdn.microsoft.com/en-us/library/vstudio/bb882637.aspx 这是有关Linq.Expressions的有用教程-http: //msdn.microsoft.com/zh-cn/library/vstudio/bb882637.aspx

the best solution is to use linqExtension with LINQKIT . 最好的解决方案是将linqExtensionLINQKIT一起使用。

    using (var context = new workEntities() )
{

    Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
    dictionary["Title"] = new List<string> {  
                    "Network Engineer", 
                    "Security Specialist", 
                    "=Web Developer"
                };
    dictionary["Salary"] = new List<string> { ">=2000" };
    dictionary["VacationHours"] = new List<string> { ">21" };
    dictionary["SickLeaveHours"] = new List<string> { "<5" };                
    dictionary["HireDate"] = new List<string> { 
                    ">=01/01/2000",
                    "28/02/2014" 
                };
    dictionary["ModifiedDate"] = new List<string> { DateTime.Now.ToString() };

    var data = context.Employee.CollectionToQuery(dictionary).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM