AsEnumerable() 对 LINQ 实体有什么影响?

发布于 2024-10-22 01:17:56 字数 1104 浏览 1 评论 0原文

阅读此处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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情旳空白 2024-10-29 01:17:56

AsEnumerable() 有效是对 IEnumerable 的强制转换,这使得成员解析查找 Enumerable 的成员可查询。当您想要强制查询的一部分作为 SQL(或类似查询)运行,而其余部分使用 LINQ to Objects 运行时,通常会使用它。

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

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

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() 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.

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?

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
              };
她如夕阳 2024-10-29 01:17:56

使用 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.

又爬满兰若 2024-10-29 01:17:56

将 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 apply where clause, ordering and projection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文