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:
- Your source is not enumerable!!
- 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