实体框架,linq函数和内存使用情况 [英] Entity Framework, linq functions and memory usage

查看:40
本文介绍了实体框架,linq函数和内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是EF的新手,我曾经使用过数据集,表适配器和存储过程.我刚发现EF的简单性,发现EF方法可以对我的开发有很大帮助.我有几个问题,我试图寻找他们的答案,但徒劳无功.由于我始终与具有巨大表的客户一起工作,因此我进行此呼叫的事实例如:

I am new to EF, I used to work with datasets, table adapters and stored procedure. I just dicovered the simplicity of EF and I found that EF approach can help me a lot with my developments. I have few questions, i tried to search for their answers but in vain. Since I always work with clients that have huge tables, the fact that I make this call for example :

_ordersContext.Services.ToList()

这是否意味着整个服务表都已加载到内存中?如果答案是肯定的(顺便说一句,我认为答案是肯定的),我们可以通过使用linq函数来避免这种内存消耗吗?例如Take()方法?(我的意思是,如果您只想拥有10条记录,而不将整个表加载到内存中).关于其他linq函数(例如where,first,firstordefault,count等)的相同问题……我的意思是,我们是否必须加载整个表?是否有很好的文档讨论如何使用就最佳实践和内存使用而言,EF.

does it mean that the whole Services table is loaded to the memory ? If the answer is yes (which by the way i think the answer is yes), can we avoid that memory cost by using linq functions? for example the Take() method ? (I mean if you want to have only 10 records, without loading the whole table in the memory). Same question about the other linq functions like where, first, firstordefault, count, etc ... I mean, do we have to load the whole table ? Is there a good documentation talking about how to use the EF in terms of best practices and memory usage.

推荐答案

在每个

Look at MSDN at every LINQ method. If you find the term deferred you know that this method does not execute the query and can be chained with others. Only those which are not using deferred execution will start processing the query and loading the result into memory.

还请记住,您可以强制 Linq-To-Objects ,而无需使用 AsEnumerable()将所有内容加载到内存中.这会将您的查询转换为sql,执行数据库查询并将结果流式传输到内存中.

Keep also in mind that you can force Linq-To-Objects without loading everything into memory with AsEnumerable(). This will translate your query into sql, perform the database query and stream the result into memory.

因此您可以执行以下操作:

So you could do something like this:

var orderList = _ordersContext.Services
    .Where(x => somecondition)
    .OrderBy(x => x.Column)
    .AsEnumerable() // after this you can use any .NET method since it doesnt need to be translated to sql
    .Where(x => complex filter not supported by Linq-To-Entities)
    .Take(10)
    .ToList()

这仍将仅将十条记录加载到内存中,并且使用数据库引擎进行(预)过滤或排序,但允许使用Linq-To-Entities不支持的.NET方法.

This will still only load ten records into memory and it uses the database engine to (pre-)filter or sorting but allows to use .NET methods which are not supported by Linq-To-Entities.

相关:

Linq-什么找出延迟执行的最快方法是什么?

通常,返回序列的方法使用延迟执行,并且返回单个对象的方法不会.

Generally methods that return a sequence use deferred execution and methods that return a single object doesn't.

异常是那些不返回诸如 ToList ToArray ToLookup ToDictionary 之类的集合的方法使用延迟执行.

Exceptions are the methods that return collections like ToList, ToArray, ToLookup, ToDictionary which don't use deferred execution.

这篇关于实体框架,linq函数和内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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