什么是AsEnumerable()的一个LINQ实体的影响? [英] What is the effect of AsEnumerable() on a LINQ Entity?

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

问题描述

读题<一个href=\"http://stackoverflow.com/questions/3389855/am-i-misunderstanding-linq-to-sql-asenumerable\">here和 给了我一些见解的情况下,它似乎就像使用在AsEnumerable是消耗内存。有没有更好的办法做到这一点LINQ和它现在做的方式是,出来的数据可靠吗?

卸下AsEnumerable的结果是本地序列不能在LINQ用于查询操作符除包含操作者的SQL实现。

  VAR结果来自中,p = pollcards.AsEnumerable()
                          加入S IN spoils.AsEnumerable()新{OCR = p.OCR中,filename = p.PrintFilename}等于新{OCR = s.seq中,filename = s.inputFileName}
                          其中,p.Version == NULL
                          排序依据s.fileOrdering,s.seq
                          选择新ReportSpoilsEntity
                          {
                              SEQ = s.seq,
                              fileOrdering = s.fileOrdering,
                              查找inputfilename = s.inputFileName,
                              OCR = p.OCR,
                              ElectorName = p.ElectorName
                          };


解决方案

AsEnumerable()有效的强制转换为的IEnumerable&LT; T&GT; ,这使得成员分辨率找到可枚举而不是可查询成员。它通常使用时要强制查询的一部分作为SQL(或类似)运行,其余的使用LINQ到对象运行。

请参阅我的<一个href=\"http://$c$cblog.jonskeet.uk/2011/01/14/reimplementing-linq-to-objects-part-36-asenumerable\">Edulinq它的博客文章了解详情。

现在你实际上得到的两个的调用 AsEnumerable 。我可以看到如何删除第一个,但不是第二个可能会造成问题,但你试图消除两者兼而有之?

  VAR结果来自中,p = pollcards
              加入S IN战利品
                 新{OCR = p.OCR中,filename = p.PrintFilename}
                 等于新{OCR = s.seq中,filename = s.inputFileName}
              其中,p.Version == NULL
              排序依据s.fileOrdering,s.seq
              选择新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
                          };

解决方案

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

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

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