将IQueryable linq查询转换为IEnumerable< T>取消linq优化的工作方式? [英] Convert an IQueryable linq query to IEnumerable<T> cancels out linq optimized way to work?

查看:69
本文介绍了将IQueryable linq查询转换为IEnumerable< T>取消linq优化的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET上的新手,我想知道linq的工作原理,因为您可以一次接一个地执行许多linq查询,但是只有在用于传输信息或转换它们之前,它们中的任何一个都不会真正执行列出等等.
有两种重要的方式来获取linq查询,方法是使用IQueryable<T>和IEnumerable,后者直接在Sql上使用where过滤器,而IEnumerable可以获取所有记录,然后在内存中使用它们.但是,让我们看一下这段代码:

I'm kinda newbie on .NET, and I was wondering how does linq works, since you can aply many linq queries, one after another, but none of them are really executed until they're used to transfer information or converted to list, etc.
There are 2 important ways to get a linq query, by using IQueryable<T>, which aplies the where filters directly on the Sql, and IEnumerable which get all the records and then it work with them on memory. However, let's take a look on this code:

            //Linq dynamic library
            IQueryable<Table> myResult = db.Categories
                .Where(a => a.Name.Contains(StringName))
                .OrderBy("Name")
                .Skip(0)
                .Take(10);

            if (myResult != null)
            {
                return myResult.AsEnumerable();
            } 
            else
            { return null; }

Depsite我正在使用Linq动态库,该查询的直接结果是在IQueryable<T>上获取的,如果该查询最终以IEnumerable的形式返回,那么该查询是否已在sql上进行了真正的过滤?还是在内存中?

Depsite i'm using Linq dynamic library, the direct result from this query is being get on IQueryable<T>, if the query is finally being returned as IEnumerable, is the query being really filtered on the sql? or is it in memory?

推荐答案

它仍将在数据库中执行,请放心.基本上,这取决于使用Where等的实现.当您通过Queryable扩展方法在IQueryable<T>上调用方法时,它将使用表达式树.当您从该查询开始获取时,它将被转换为SQL并发送到数据库.

It's still going to execute in the database, don't worry. Basically it's all down to which implementation of Where etc is used. While you're calling methods on IQueryable<T> - via the Queryable extension methods - it will be using expression trees. When you start to fetch from that query, it will be turned into SQL and sent to the database.

另一方面,如果在之后使用任何这些方法,则将其作为IEnumerable<T>(就编译时类型而言),它将使用扩展方法在Enumerable中,其余所有处理 将在处理中完成.

On the other hand, if you use any of those methods after you've got it as an IEnumerable<T> (in terms of the compile-time type), that will use the extension methods in Enumerable, and all of the rest of the processing would be done in-process.

作为一个例子,考虑一下:

As an example, consider this:

var query = db.People
              .Where(x => x.Name.StartsWith("J"))
              .AsEnumerable()
              .Where(x => x.Age > 20);

此处AsEnumerable() just 返回其输入序列,但键入为IEnumerable<T>.在这种情况下,数据库查询将仅返回名称以J开头的人员-然后将在客户端进行年龄过滤.

Here AsEnumerable() just returns its input sequence, but typed as IEnumerable<T>. In this case, the database query would return only people whose name began with J - and then the age filtering would be done at the client instead.

这篇关于将IQueryable linq查询转换为IEnumerable&lt; T&gt;取消linq优化的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆