Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have declared an anonymous type with 2 elements as follows and later filled it. Code is condensed of course.

VB
public StudentNames

Sub FillStudents()
        Dim st = From sts In db.StudentLists Order By studs.Last, studs.First _
                  Select studs.FullName, studs.StudentId

        StudentNames = sts.ToArray
End Sub


How do I get the value of each element
Tried using StudentNames(0,1) and get "Attempted to operate on an array with the incorrect number of dimensions."

I know I could declare StudentNames as a two dimensional array and loop through each st to fill the array, but sts.ToArray is so simple. It works perfect if I use a single dimensioned anonymous type array, but I would like to save the StudentId for later use.

There are a lot of other solutions but this one uses a lot less code both now and later in the program (if I can get it to work).

By the way, I've searched info on anonymous types but they all contain a key, such as:
Dim StudentNames = New With {Key .Name = st.FullName, .Stid = st.StudentId}
which still requires iterating through the query. I would prefer avoiding that if I can.
Posted
Updated 30-Oct-14 5:43am
v2

1 solution

Why would you assume that the Linq would return a two dimensional array? That's how you are trying to access it!
What it returns is a one dimensional array of an anonymous type which has two members, so you can use StudentNames(0) to return you an instance of the anonymous type, but you can't access the type members as if they were array elements!

Me? I'd declare a "helper class" and return an array of that if I wanted to use it outside the subroutine.
 
Share this answer
 
Comments
Andrew Alix 31-Oct-14 9:34am    
You're right. If I had used StudentNames = st, I would have had the same and I wouldn't have been confused by the ".toArray" property.
I've programmed in several "old" languages for years and am totally self-taught in VB.net, which leaves me with holes as you can imagine.

I've done some research into helper classes, but am not sure how to implement them. Right now I use a "Linq to SQL" clase to access the data in all tables easily.

How would a helper class benefit me here?
OriginalGriff 31-Oct-14 9:45am    
A helper class would just be a named version of the anonymous type you are creating with the Linq Select - so you could pass the collection of the helper class as a strongly typed unit instead of hoping that the framework can sort out what you are trying to pass through into a method.
Just declare a class with only the members you need, and use Select New to create an instance in the Linq statement.
Andrew Alix 31-Oct-14 16:37pm    
Thanks so much for your answer. I'm going to try the helper class and see how it goes.

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