AsEnumerable() 对 LINQ 实体有什么影响?
阅读此处和here 让我对这种情况有了一些了解,而且似乎使用 AsEnumerable 很消耗内存。有没有更好的方法来执行此 LINQ 以及现在完成的方式,得出的数据可靠吗?
删除 AsEnumerable 会导致“除 Contains 运算符之外的查询运算符的 LINQ to SQL 实现中无法使用本地序列”。
var results = from p in pollcards.AsEnumerable()
join s in spoils.AsEnumerable() on new { Ocr = p.OCR, fileName = p.PrintFilename } equals new { Ocr = s.seq, fileName = s.inputFileName }
where p.Version == null
orderby s.fileOrdering, s.seq
select new ReportSpoilsEntity
{
seq = s.seq,
fileOrdering = s.fileOrdering,
inputFileName = s.inputFileName,
Ocr = p.OCR,
ElectorName = p.ElectorName
};
Reading the questions here and here has given me some insight into the situation, and it seems like using the AsEnumerable is memory consuming. Is there a better way to do this LINQ and the way it is done now, is the data that comes out reliable?
Removing the AsEnumerable results in a "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator."
var results = from p in pollcards.AsEnumerable()
join s in spoils.AsEnumerable() on new { Ocr = p.OCR, fileName = p.PrintFilename } equals new { Ocr = s.seq, fileName = s.inputFileName }
where p.Version == null
orderby s.fileOrdering, s.seq
select new ReportSpoilsEntity
{
seq = s.seq,
fileOrdering = s.fileOrdering,
inputFileName = s.inputFileName,
Ocr = p.OCR,
ElectorName = p.ElectorName
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AsEnumerable()
有效是对IEnumerable
的强制转换,这使得成员解析查找Enumerable
的成员可查询
。当您想要强制查询的一部分作为 SQL(或类似查询)运行,而其余部分使用 LINQ to Objects 运行时,通常会使用它。请参阅我的Edulinq 博客文章 了解更多信息。
现在,您实际上已经对
AsEnumerable
进行了两次调用。我可以看到删除第一个而不是第二个可能会导致问题,但是您是否尝试过删除两个?AsEnumerable()
is effectively a cast toIEnumerable<T>
, which makes member resolution find members ofEnumerable
instead ofQueryable
. It's usually used when you want to force part of a query to run as SQL (or similar), and the remainder to run using LINQ to Objects.See my Edulinq blog post on it for more information.
Now you've actually got two calls to
AsEnumerable
. I can see how removing the first but not the second could cause problems, but have you tried removing both?使用 AsEnumerable 将中断查询并将“外部部分”作为 linq-to-objects 而不是 Linq-to-SQL。实际上,您正在为两个表运行“select * from ...”,然后在客户端执行连接、where 子句过滤、排序和投影。
Using AsEnumerable will break off the query and do the "outside part" as linq-to-objects rather than Linq-to-SQL. Effectively, you're running a "select * from ..." for both your tables and then doing the joins, where clause filter, ordering, and projection client-side.
将 AsEnumerable 与实体框架一起使用时要小心;如果您的表有大量数据,您的查询可能会很慢,因为查询将首先加载数据,然后应用
where
子句、排序和投影。Be careful when using
AsEnumerable
with Entity Framework; if your table has lots of data your query may be slow because the query will first load data and then applywhere
clause, ordering and projection.