Annoying "feature" when using Linq to XML in Silverlight projects

I know this has a simple fix, but the error message might not be too clear to some people. If you try to use Linq to XML you must first start off by adding a using statement for System.Linq.Xml. This is very easy to do... Just write the initial XDocument, with the right casing, and then press "Ctrl+." and then enter. This will add the using statement and you can keep going. So you start writing your query but it doesn't work.

You might for example write:
var myQuery = from e in doc.Descendants(XName.Get("myElementName")...

But you get a nasty error and a red squiggly line appears under the doc.Desc... statement.

'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)

If you look at the message, it doesn't say to much and I have now run into it 3 times. I guess I never learn... First I start off by checking my references to System.Xml.Linq and System.Xml... Both are OK... So why is it complaining? Well, the message also asks about a reference to System.Core. So I check that and it is ok. So it isn't that...

Solution: You need a using statement for System.Linq. Why? Well, I guess some of the extension methods you need for your Linq expression is in there. I thought it was enough to just have the assemblies referenced for the extension methods to work, but apparently not. I should probably look into this a bit more and figure out how it really works... But I wont... I have WAY to much to do, and I have already solved the problem.

Comments are closed