动态linq到实体,如何基于变量构建查询[英] Dynamic LINQ to Entities, how to build query based on variables

本文是小编为大家收集整理的关于动态linq到实体,如何基于变量构建查询的处理方法,想解了动态linq到实体,如何基于变量构建查询的问题怎么解决?动态linq到实体,如何基于变量构建查询问题的解决办法?动态linq到实体,如何基于变量构建查询问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我需要构建的查询是:

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

唯一的问题是,s.title和s.episodetitle是动态的.

意味着以下变量可能是查询的一部分:

(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)

例如

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

在任何类型的组合中.

我如何构建此查询,而不必考虑这里的每种可能性?

推荐答案

如果您只需要AND逻辑,您只能针对需要搜索的每个属性重复调用.Where().

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

如果您的查询始终具有一定的结构,并且您想忽略null搜索值,则可以进行一个大查询,但短路属性比较与空检查.

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))))
        );

但是,如果您需要对查询的完全控制" rel =" nofollow"> system.linq.expressions 构建特定查询以搜索必要的属性.这是linq..xexpressions的有用教程 - 其他推荐答案

最好的解决方案是使用 linQextension linqkit .

    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();
}

本文地址:https://www.itbaoku.cn/post/1557818.html

问题描述

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.

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)

e.g.

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.

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. Here is a useful tutorial on Linq.Expressions - http://msdn.microsoft.com/en-us/library/vstudio/bb882637.aspx

其他推荐答案

the best solution is to use linqExtension with LINQKIT.

    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();
}
查看更多