0

我尝试了许多免费提供的代码转换器来转换以下部分,但是没有成功。

 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()

我想我在这里得到它

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();
4

3 回答 3

1

您的转换遗漏了UCase,其在 C# 中的等价物是ToUpperCase.

但是,这不是执行不区分大小写的字符串比较的推荐方法。

e.Name.LocalName == searchName

应该用类似的东西代替:

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

还有,什么是propertyName?不管它是什么,它的价值显然不依赖于e. 您在e第一个查询中为每个查询选择一个相同的东西,这是没有意义的。我猜你的意思是select e

那么您可能想要的是以下内容:

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();

我只是不确定如何Select <<%= propertyName %>><%= a.Value %></>用 C# 表示,因为我不懂 VB。

于 2012-02-24T10:58:16.237 回答
0

我一直使用的好的在线代码转换器是http://www.developerfusion.com/tools/convert/vb-to-csharp/

于 2012-02-24T10:53:52.507 回答
0

我在一次演示中从 Microsoft 的Roslyn项目中获悉。也许这可以帮助你。

微软希望将Roslyn集成到未来的 Visual Studio 版本中。然后可以将代码从 VB 复制到剪贴板并将其粘贴为 C# 代码。去年也有一个关于这个的演讲,也许是同一个。

于 2012-02-24T11:15:05.737 回答