繁体   English   中英

如何将C#Linq转换为VB.NET

[英]How to Convert C# Linq to VB.NET

有人可以帮我把这个C#Linq-u转换成VB.NET代码

var groupedItems = from item in LinqueResult
                    orderby item.Category
                    group item by 
                        item.GetType().GetProperty("Test").GetValue(item).ToString()
                        into groupPropertie 
                    select new KeyedList<string, ItemToDisplay>(groupPropertie);

谢谢

更新:

ken2K我知道,但我没有得到在线转换器的工作代码

到目前为止,我自己得到了它

Public Function GroupedPhotos(LinqueResult As List(Of ItemToDisplay), GroupMember As [String]) As List(Of KeyedList(Of String, ItemToDisplay))

    Dim groupedItems = From groupPropertie In From item In LinqueResult
                                              Order By item.Category
                                              Group item By item.GetType.GetProperty(GroupMember).GetValue(item).ToString() Into Group
                                              Select New KeyedList(Of String, ItemToDisplay)(groupPropertie)

    Return New List(Of KeyedList(Of String, ItemToDisplay))(groupedItems)
End Function

我得到这个错误:

错误1:范围变量名称与“对象”类的成员名称不匹配。 C:\\ xxx \\ MainPage.xaml.vb 53 118 LongListSelectorFreeLancVBasic

在线转换器很糟糕。 正确的翻译是:

Dim groupedItems = from item in LinqueResult
                   order by item.Category
                   let test = item.GetType().GetProperty("Test").GetValue(item).ToString() 
                   group item by test into groupPropertie = Group
                   select new KeyedList(Of string, ItemToDisplay)(groupPropertie)

请注意,您必须使用let子句将item.GetType()...ToString()的结果绑定到另一个名称。 否则,VB.Net尝试创建一个名为ToString的局部变量,然后抱怨ToString不能使用,因为在Object上有一个具有此名称的成员。

在这条长行上使用let可以更容易地阅读恕我直言。

group语法也不同:要使用命名组,您必须使用your_group_name = Group 但由于你实际上没有对groupProertie做任何事情,你也可以使用

...
group item by test into Group
select new KeyedList(Of string, ItemToDisplay)(Group)

尝试这个

Dim groupedItems = From groupPropertie In From item In LinqueResultOrder By item.CategoryGroup item By item.[GetType]().GetProperty("Test").GetValue(item).ToString()New KeyedList(Of String, ItemToDisplay)(groupPropertie)

将编译后的版本粘贴到任何反编译器( DotNetPeekJustDecompile或反射器)中,然后让它向您显示VB中的代码

链接的东西几乎是一样的,除了vb.net通常是关键字的大写:

Dim groupedItems = From item In LinqueResult
                    OrderBy item.Category
                    Group item By item.GetType()
                        .GetProperty("Test")
                        .GetValue(item)
                        .ToString() 
                    Into groupPropertie 
                    Select New KeyedList(Of String, ItemToDisplay)(groupPropertie)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM