This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Tuesday, April 27, 2010

Resolving LINQ Error: Missing Query Pattern Implementation

There are a couple of errors that I have come up in working with LINQ and LINQ to SQL that have common roots.  There are a couple of easy fixes when you run into the following error:

Could not find an implementation of the query pattern for source type <SomeType>.

You will also receive some further information about the error, usually along the lines of one of the following:

  • 'Where' not found.
  • 'Select' not found.

Fix 1: LINQ Using Missing

This one is easy: just make sure you have the following using statement in your class file:

image

Fix 2: Got Members?

Use the correct member of the object you are performing a query on, and make sure you’re not intending to use a property of mehod of that type.

For Example, where _dc is a DataContext object for LINQ to SQL, I have absent-mindedly forgotten the table reference in the query:

image

The above should be written as follows to avoid the query pattern implementation error:

image

That one can be a little more tricky because – especially if you’re coding late – the compiler error doesn’t really lead you to a ‘I missed a property reference’ with that error.

Fix 3: Wrong Type

Make sure you are trying to query an object that works with LINQ.  Specifically, LINQ to objects will need to have an implementation of the IEnumerable interface in the object that it is trying to query.

If you run into something that is called GetNameList, but it returns a delimited string instead of (the expected) List<string>, you can still query it after you take a simple step and do the split:

image

…and those are the most common ways to solve issues around the missing implementation of the query pattern for ‘x’ problem.

No comments:

Post a Comment