简体   繁体   中英

How to Convert C# Linq to VB.NET

can someone help me convert this C# Linq-u to a VB.NET code

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

Thanks

UPDATE:

ken2K i know but i dont get a working code with the online converters

I got it on my own to this point

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

And i get this error:

Error 1: Range variable name cannot match the name of a member of the 'Object' class. C:\\xxx\\MainPage.xaml.vb 53 118 LongListSelectorFreeLancVBasic

Online converters suck. The correct translation is:

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)

Note that you have to use a let clause to bind the result of the item.GetType()...ToString() to another name. Otherwise, VB.Net tries to create a local variable named ToString and then complains about ToString can't be used because there's a member on Object with this name.

Using a let on this long line makes it easier to read IMHO.

The group syntax is also different: to use a named group, you'll have to use your_group_name = Group . But since you actually don't do anything with groupProertie , you could as well just use

...
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中的代码

The link stuff is pretty much the same, except for vb.net typically uppercases the keywords:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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