0

I have tried a number of freely available code converters to convert the following piece, however without success.

 Dim resultList = ((From e In p_Xml.Elements()
                       Where UCase(e.Name.LocalName) = searchName).Union(
                         From a In p_Xml.Attributes()
                         Where UCase(a.Name.LocalName) = searchName
                         Select <<%= propertyName %>><%= a.Value %></>)).ToList()

I think I got it here

var resultList = (from e in p_xml.Elements()
                where e.Name.LocalName == searchName
                select propertyName).
Union(from a in p_xml.Attributes()
      where a.Name.LocalName == searchName
      select a.Value).ToList();
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
Rauld
  • 980
  • 2
  • 10
  • 19

3 Answers3

1

Your conversion left out UCase, whose equivalent in C# is ToUpperCase.

This is not the recommended way to perform case-insensitive string comparisons, though.

e.Name.LocalName == searchName

should be replaced with something like:

String.Compare(e.Name.LocalName, searchNamename, StringComparison.InvariantCultureIgnoreCase) == 0

Also, what is propertyName? Whatever it is, its value is obviously not dependent on e. You're selecting one and the same thing for every e in your first query, which makes no sense. I guess you meant select e.

What you probably want then is something along the lines of:

var resultList = (from e in p_xml.Elements()
                where String.Compare(e.Name.LocalName, searchName, StringComparison.InvariantCultureIgnoreCase) == 0
                select e).
Union(from a in p_xml.Attributes()
      where String.Compare(a.Name.LocalName, searchName, StringComparison.InvariantCultureIgnoreCase) == 0
      select a.Value).ToList();

I'm only not sure how to represent Select <<%= propertyName %>><%= a.Value %></> in C#, since I don't know VB.

Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
0

Good online Code Converter that I use all the time is http://www.developerfusion.com/tools/convert/vb-to-csharp/

kamui
  • 3,339
  • 3
  • 26
  • 44
munnster79
  • 578
  • 3
  • 16
  • It does not really help him (it reports an InvalidOperationException when attempting to convert his code sample), plus he mentioned that he `tried a number of freely available code converters`, and when I google "code converter", your link comes second on the search results list ( first for the query "c# vb code converter"), which means it is rather unlikely that he's not aware of that website... – Konrad Morawski Feb 24 '12 at 11:23
0

I heard from Microsoft's Roslyn project in a presentation. Maybe that can help you.

Microsoft want to integrate Roslyn in a future Visual Studio version. Then it would be possible to just copy code from VB to clipboard and paste it as C# code. There was also a presentation about that in last year, maybe the same one.

brgerner
  • 4,287
  • 4
  • 23
  • 38