Drop in the Bucket
One coder in the sea of s/w knowledge

Please Read the Error Message

Thursday, May 29, 2008 11:15 PM

Generic List Doesn’t Support the Query Pattern

I recently wrote similar code to this little fragment (this is a sanitized version for the purpose of this example).

 

   12 
   13 List<int> nums = new List<int> { 0, 1, 1, 2,
   14                     3, 5, 8, 13, 21 };
   15 
   16 var evenQ = from n in nums
   17             where n % 2 == 0
   18             select n;
   19 

 

I was shocked when I received this error message

WTF! What do you mean, a list doesn’t support the query pattern!? That’s absolutely ridiculous. I’m quiting C# development, and I’m converting to writing Rails apps on the Mac….

We interrupt this moment of panic for the following public service announcement:

Developers, do you get tired of reading those pesky error messages? Me too, but remember the compiler team has gone through a lot of trouble to tell you when you’re an idiot and help you recover. Take it from me, if you read the entire error me sage instead of the first half sentence, you’ll be better informed of why you’re such a bonehead and more likely to recover. Now back to our regularly scheduled moment of sanity.

...”missing a using directive?”. Yes, as it turns out I prematurely used one of my favorite VS features: remove and sort using statements.

All is well, and the universe is in a causal state again. Turns out that if you don’t have any dependencies on statements or extensions in System.Linq “remove and sort” will remove the LINQ using statement (duh), and thus all the LINQ goodness will be hidden from the compiler.

Simply adding using System.Linq; fixed this particular goof. Yes, perhaps I’ll read the entire error message next time.

Comments have been closed on this topic.