Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the effect of AsEnumerable() on a LINQ Entity?

Tags:

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                           }; 
like image 206
Andy Avatar asked Mar 15 '11 11:03

Andy


People also ask

What is the use of AsEnumerable in LINQ?

In LINQ, AsEnumerble() method is used to convert the specific type of given list to its IEnumerable() equivalent type.

Does AsEnumerable execute the query?

Calling AsEnumerable( ) does not execute the query, enumerating it does. IQueryable is the interface that allows LINQ to SQL to perform its magic.

What is the difference between AsEnumerable and AsQueryable?

AsEnumerable preserves deferred execution and does not build an often useless intermediate list. On the other hand, when forced execution of a LINQ query is desired, ToList can be a way to do that. AsQueryable can be used to make an enumerable collection accept expressions in LINQ statements.

When should I use IQueryable and IEnumerable using LINQ?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.


1 Answers

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               }; 
like image 115
Jon Skeet Avatar answered Oct 23 '22 08:10

Jon Skeet