LINQ: Error 19 Could not find an implementation of the query pattern for source type

by matt 25. January 2008 10:12

Overview

I reckon that I have probably seen the same LINQ demo as just about everyone else in the world.  You know, the one where you get a list of all of the processes running on the server and print them out to the console as follows:

var processes = from activeProcesses in Process.GetProcesses() 
                select new { Name = activeProcesses.ProcessName, ProcessId = activeProcesses.Id };

foreach (var row in processes) Console.WriteLine(row.ToString());

Console.ReadLine();

Well, I tired to use the same principle to iterate through a custom collection for an idea I was trying out and I got a heavily descriptive error of:

"Error 19 Could not find an implementation of the query pattern for source type"

So what does that mean?

Solution

It can actually mean a couple of things depending on the full error message.  The two main causes that I have noticed are:

  1. Your source is not enumerable!!
  2. LINQ doesn't have a clue what your source is.

The first issue is fairly easily addressed, make sure you use enumerable types.  It turns out that in the example I was trying to make work, I'd used the wrong object as the source which was why I got the error in the first place. 

Once I had changed to the correct object, I still had the error.  The difference was that the error message now also included the following

"Consider explicitly specifying the type of the range variable"

Interesting I thought, why on earth would that be?  The code I had originally was as follows:

var pollingDirectories = from directory in serviceConfigSection.PollingDirectoires 
                select new { DirectoryPath = directory.Path, FileType = directory.FileType };

foreach (var row in pollingDirectories ) Console.WriteLine(row.ToString());

Console.ReadLine();

What I need to to was to tell LINQ the type of source explicitly so that it would know what to do with it.  So I needed to make my code look as follows:

var pollingDirectories = from DirectoryElement directory in serviceConfigSection.PollingDirectoires 
                select new { DirectoryPath = directory.Path, FileType = directory.FileType };

foreach (var row in pollingDirectories ) Console.WriteLine(row.ToString());

Console.ReadLine();

After that - everything seems to work just fine. 

I guess in some cases, LINQ just needs to be given a little more information about what it is doing. 

References

Versions

Metadata

  • Categories: .Net Framework 3.5, Visual Studio 2008, Orcas, LINQ
  • Additional Keywords: Orcas, LINQ, Error 19, Enumerable, IEnumerable

Tags:

.Net Framework | LINQ

Comments

5/15/2008 3:46:08 AM #

Hey...this was the MOST valuable piece of information I have learned about LINQ.

I used it on a piece of software called SpreadsheetGear to Sum up some columns...spent hours, saw this...spent seconds...now it works great !

Thanks;

Keith

Keith Garsson |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar