简体   繁体   English

在ASP.Net MVC中,当实现搜索时,应该将where子句添加到ienumerable还是iqueryable中?

[英]In ASP.Net MVC should I add my where clause to my ienumerable or iqueryable when implementing search?

I was wondering when a user goes to an index page for a table on my website and uses a search function should I add my where clause to the ienumerable or iqueryable? 我想知道用户何时进入我网站上表的索引页面并使用搜索功能,是否应该将where子句添加到ienumerable或iqueryable? I know both can be done but which one is more efficient and can I add a custom search string extension to a iqueryable ? 我知道这两种方法都可以完成,但是哪一种效率更高,可以向iqueryable添加自定义搜索字符串扩展吗?

Adding it to the IQueryable will add it to the query that goes to the database since it's part of a live connection to the DB. 将其添加到IQueryable会将其添加到数据库查询中,因为它是与数据库的实时连接的一部分。

Adding it to an IEnumerable(that is not of type IQueryable) will do the filtering in memory after the query has executed. 执行查询后,将其添加到IEnumerable(不是IQueryable类型)将在内存中进行过滤。

If you're dealing with an underlying IQueryable, in both cases, I doubt there will be a difference. 如果您要处理基础IQueryable,则在两种情况下,我都怀疑会有所不同。 However, I recommend viewing the resulting queries in a sql profiler to ensure you are executing the expected queries. 但是,建议您在sql探查器中查看结果查询,以确保您正在执行预期的查询。

I'd add it to your IQueryable just in case, but since both are interfaces, it may not really matter. 我以防万一,将其添加到您的IQueryable中,但是由于这两个都是接口,因此可能并不重要。 If your IEnumerables are always IQueryables, you should still get the benefits of your queries being run on the database and not after the database returns results. 如果您的IEnumerables始终是IQueryables,您仍应获得在数据库上而不是在数据库返回结果之后运行查询的好处。

IQueryable and IEnumerable are what's called "interfaces". IQueryableIEnumerable称为“接口”。 An interface defines an API that the actual object should be able to respond to, while allowing for a degree of variability in the actual type (read: class that the object implements). 接口定义了实际对象应该能够响应的API,同时允许实际类型 (阅读:对象实现的类)具有一定程度的可变性。 As long as the type implements the interface, everything just works, and you can (for the most part) swap one type for another as long as they all implement the same interface(s). 只要该类型实现了接口,一切都将起作用,并且(大多数情况下)您可以将一种类型交换为另一种类型,只要它们都实现相同的接口即可。

That said, IQueryable is actually an extension of IEnumerable , so for the purposes of what you're talking about here, they're practically the same thing. 就是说, IQueryable实际上是IEnumerable的扩展,因此就您在此处所讨论的目的而言,它们实际上是同一件事。 In other words, it's not about whether the returned object set from the database is cast to an IQueryable or an IEnumerable ; 换句话说,这与从数据库返回的对象集是否IQueryable转换为IQueryableIEnumerable in either case, the query may or may not have been executed yet, as neither interface dictates that the query must be executed. 在这两种情况下,查询都可能已经执行,也可能尚未执行,因为这两个接口都不指示必须执行查询。 What does matter is whether you've done something in the code that would cause the query to be executed: things like casting to a list ( .ToList() ) or iterating over the result set ( for , foreach , etc.), etc. Running a count will also execute the query, but for a slightly different reason (you'll issue a COUNT query to the database instead of a SELECT). 重要的是您是否在代码中做了某些操作导致查询被执行:诸如转换为列表( .ToList() )或遍历结果集( forforeach等)之类的事情,等等。 。运行计数也将执行查询,但是出于稍微不同的原因(您将向数据库发出COUNT查询,而不是SELECT)。

The moral is that you just need to pay attention to which parts of your code will actually cause the query to be executed and make sure any query alterations occur before that point. 道理是,您只需要注意代码的哪些部分实际上将导致查询被执行,并确保在此之前发生任何查询更改。

From your description of the situation I'd use an IQueryable to run your Where() 's against then cast it as a List<SomeViewModel> to return it to your page. 根据您对情况的描述,我将使用IQueryableWhere()进行操作,然后将其List<SomeViewModel>List<SomeViewModel>以将其返回到您的页面。 Doing this will defer execution of the query against the database until you've refined it to return just what you need. 这样做将推迟对数据库的查询执行,直到您对查询进行了细化以返回所需的内容为止。

Instead of custom search string extensions I use plain old variables and pare down my object depending on what gets passed in. So if you're writing a search controller that takes in firstName and lastName as parameters you'd do something like: 代替自定义搜索字符串扩展,我使用普通的旧变量并根据传入的内容缩减对象。因此,如果您要编写一个将firstNamelastName作为参数的搜索控制器,则可以执行以下操作:

IQueryable<User> users = Context.Users;

if (firstName != null)
    users = users.Where(x => x.FirstName == firstName);

if(lastName != null)
    users = users.Where(x => x.LastName == lastName);

List<SearchResultViewModel> model = users.Select(x => new SearchResultViewModel
        {
            FirstName = x.FirstName,
            LastName = x.LastName,
            JobTitle = x.JobTitle
            // and so forth for whatever fields you need to return...
        }).ToList();
        // The ToList() will cause the query to be 
        // evaluated and executed.

Naturally, you should check to make sure at least one of your search fields has something in it so you don't return the whole table's worth of data. 自然地,您应该检查以确保其中至少一个搜索字段中包含某些内容,这样就不会返回整个表的数据价值。 From here you can rig it to accept variables for pagination, apply sorting, all kinds of fun stuff. 在这里,您可以绑定它来接受用于分页的变量,应用排序以及各种有趣的东西。

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

相关问题 在ASP.NET MVC 5中可查询到IEnumerable - IQueryable to IEnumerable in ASP.NET MVC 5 我应该在哪里存储我的临时视图模型在 asp.net MVC 中? - Where should i store my temporary view model in asp.net MVC? 我应该何时从我认为的数据库中查询ASP.NET MVC - When should I query from a database in my view, ASP.NET MVC 如何用我的IEnumerable填充/填充DropDown <Model> 值ASP.NET MVC - How can I fill / populate DropDown with my IEnumerable<Model> value ASP.NET MVC 我应该如何处理我的 asp.net 核心 mvc 应用程序的日期范围内的过滤搜索结果? - How should I handle filtering search results within a date range for my asp.net core mvc app? 什么时候应该使用 IEnumerable,什么时候应该使用 IQueryable? - When should I use IEnumerable and when IQueryable? 我应该如何存储我的ASP.NET MVC网站的设置? - How should I store my ASP.NET MVC site's settings? 我应该如何构建我的视图模型 - ASP.NET MVC - How should i structure my View Model - ASP.NET MVC 我应该在哪个ASP.NET MVC应用程序层检查成员身份信息? - Which tier of my ASP.NET MVC application should I be checking Membership information? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM