AsEnumerable() 对 LINQ 实体有什么影响? [英] What is the effect of AsEnumerable() on a LINQ Entity?

查看:36
本文介绍了AsEnumerable() 对 LINQ 实体有什么影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读问题此处here 让我对这种情况有了一些了解,似乎使用 AsEnumerable 会消耗内存.有没有更好的方式来做这个 LINQ 以及现在的方式,出来的数据可靠吗?

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?

删除 AsEnumerable 会导致本地序列不能在 LINQ to SQL 的查询运算符实现中使用,但包含运算符除外."

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

推荐答案

AsEnumerable()有效转换为 IEnumerable,这使得成员解析查找 Enumerable 而不是 Queryable 的成员.当您希望强制一部分查询以 SQL(或类似方式)运行,而其余部分使用 LINQ to Objects 运行时,通常会使用它.

AsEnumerable() is effectively a cast to IEnumerable<T>, which makes member resolution find members of Enumerable instead of Queryable. 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.

请参阅我的 Edulinq 博客文章 了解更多信息.

See my Edulinq blog post on it for more information.

现在您实际上已经两次调用了AsEnumerable.我可以看到删除第一个而不是第二个会导致问题,但是您是否尝试删除两者?

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?

var results = from p in pollcards
              join s in spoils
                 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
              };

这篇关于AsEnumerable() 对 LINQ 实体有什么影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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