Converting Linq to XML query from C# to VB.Net. Can you spot my error?

I used the following site to convert: developerfusion.com/tools/convert/csharp... Dim courses As List(Of Course) = (From course In CourseXML. Descendants(ns & "row") _ Â Â Join coursecategory In CourseCategoryXML. Descendants("Table") On DirectCast(course.

Attribute("code"), String) = DirectCast(coursecategory. Element("DATA"), String) _ Â Â Join category In CategoryXML. Descendants("Table") On DirectCast(coursecategory.

Element("GRP"), String) = DirectCast(category. Element("GRP"), String) _ Â Â Where DirectCast(coursecategory. Element("RECTYPE"), String) = "C" _ Â Â Select New Course()).

ToList(Of Course)() The main difference is the last Select.

I used the following site to convert: developerfusion.com/tools/convert/csharp... Dim courses As List(Of Course) = (From course In CourseXML. Descendants(ns & "row") _ Â Â Join coursecategory In CourseCategoryXML. Descendants("Table") On DirectCast(course.

Attribute("code"), String) = DirectCast(coursecategory. Element("DATA"), String) _ Â Â Join category In CategoryXML. Descendants("Table") On DirectCast(coursecategory.

Element("GRP"), String) = DirectCast(category. Element("GRP"), String) _ Â Â Where DirectCast(coursecategory. Element("RECTYPE"), String) = "C" _ Â Â Select New Course()).

ToList(Of Course)() The main difference is the last Select.

I actually used the same site for the initial conversion. Very handy. However, the current tool doesn't deal well with LINQ yet.

But +1 since this is certainly a relevant site for others. – Cory House Dec 3 '09 at 1:06.

You are getting different results because of the casts. Coursecategory. Element("RECTYPE").

Value returns a different result than (string)coursecategory. Element("RECTYPE") == "C" (and obviously CType(coursecategory. Element("RECTYPE"), String) = "C").

If one of your elements is missing a child RECTYPE node, you will not have any results if you properly cast it, which you did in VB.NET. In C# you didn't cast (string)coursecategory. Element("RECTYPE") == "C" right.

If you use ToString() or . Value you would get the correct results. Use coursecategory.

Element("RECTYPE"). Value instead, and avoid the casts altogether. You could test this by changing your select to return: select new { Wrong = (string)coursecategory.

Element("RECTYPE"), // No exception ... incorrect result! //Maybe = coursecategory. Element("RECTYPE").ToString() //throws NullReferenceException //Right = (string)coursecategory.

Element("RECTYPE"). Value // throws NullReferenceException because of a missing element. CategoryCode = category.

Element("GRP"). Value, Code = course. Attribute("code").

Value }).

XElement has static explicit operator string(XElement) defined in it, so (string)Element("Foo") is exactly equivalent to Element("Foo").ToString(), except that the former won't throw on null. – Pavel Minaev Dec 1 '09 at 2:52 ... sorry, equivalent to Element().Value. – Pavel Minaev Dec 1 '09 at 2:54 I tested the code on data they are not equivalent.

– Richard Hein Dec 1 '09 at 3:56 "(string)Element("Foo") is exactly equivalent to Element("Foo").ToString(), except that the former won't throw on null", so therefore they are not exactly equivalent and you've proven my point. – Richard Hein Dec 1 '09 at 3:59 Thanks for the reply Richard, I actually found my call to the method this Linq query is in was bogus so the issue wasn't even with the query itself. However, regarding throwing an exception, I'd actually prefer the joins operate like an inner join.So which solution are you suggesting in that case?

– Cory House Dec 1 '09 at 14:02.

(from course in CourseXML. Join coursecategory in CourseCategoryXML. Descendants("Table") on (string)course.

Attribute("code") equals (string)coursecategory. Join category in CategoryXML. Descendants("Table") on (string)coursecategory.

Element("GRP") equals (string)category.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions