Click here to Skip to main content
15,909,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to convert a bit of code from C# to VB.NET that's looking for a specific set of class members and I've got it almost completely worked out except for a little Linq query that uses some C# syntax with which I'm unfamiliar:

var members = entityType.GetTypeInfo().GetMembers(bindingFlags)
	.Where(m => m.MemberType == MemberTypes.Property || m.MemberType == MemberTypes.Field)
	.Select((m, i) => (member: m, positions: (user: resolver.GetPosition(m), builtin: i)))
	.Where(x => x.positions.user != -1)
	.OrderBy(x => x.positions)
	.Select(x => x.member)
	.ToArray();


What I have tried:

The first .Where() bit makes sense and converts to:
.Where(Function(m) m.MemberType = MemberTypes.Property Or m.MemberType = MemberTypes.Field)

But, it's the first .Select() piece that I don't understand. It seems to be defining some labels (or perhaps a temporary structure of some sort) that it can pass along to the next .Where() clause. I've tried to define a function where I declare some variables with these names, but I can't figure out how to declare them:
.Select(Function(m, i)
			Dim member = m
			Dim positions As New Dictionary(Of Integer, Integer)(Resolver.GetPosition(m), i)
		End Function)

The types should be correct (both Integers), but obviously the attempt to just create a Dictionary(Of Integer, Integer) doesn't give me the ability to move into the Next .Where() or the .OrderBy() clauses with the "names" propegating.

Do I need to create/declare a Private Structure or something in the Class that I can use in that query?
Posted

1 solution

use anonymous types


Dim members = entityType.GetType().GetMembers(bindingFlags) _
    .Where(Function(m) m.MemberType = MemberTypes.Property OrElse m.MemberType = MemberTypes.Field) _
    .Select(Function(m, i) New With {
                .Member = m,
                .Positions = New With {
                    .User = resolver.GetPosition(m),
                    .Builtin = i
                }
            }) _
    .Where(Function(x) x.Positions.User <> -1) _
    .OrderBy(Function(x) x.Positions.Builtin) _
    .Select(Function(x) x.Member) _
    .ToArray()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900