Load Two Dimensional Array from LINQ Query?
I’m using VB.Net and have the following LINQ Query…
Dim ACFTTail5 = (From tailcounts In db.TailCounts _
Where tailcounts.Model = "UH-60A" _
Group tailcounts By _
tailcounts.Tail, tailcounts.Model _
Into g = Group Order By Tail _
Select Tail, rdf = CStr(g.Sum(Function(p) _ p.TailCount))).ToArray
I’m trying to get a two dimensional array like below..
ACFTTail5 (0)(0) “Tail-a”
ACFTTail5 (0)(1) “RDF-a”
ACFTTail5 (1)(0) “Tail-b”
ACFTTail5 (1)(1) “RDF-b”
ACFTTail5 (2)(0) “Tail-c”
ACFTTail5 (2)(1) “RDF-c”
ETC..
I have tried…
Dim ACFTTail5(,) As String
ACFTTail5 = (From tailcounts In db.TailCounts _
Where tailcounts.Model = "UH-60A" _
Group tailcounts By _
tailcounts.Tail,开发者_JAVA百科 tailcounts.Model _
Into g = Group Order By Tail _
Select Tail, rdf = CStr(g.Sum(Function(p) p.TailCount)))
But get the following error…
“Unable to cast object of type 'System.Data.Linq.DataQuery1[VB$AnonymousType_4
2[System.String,System.String]]' to type 'System.String[,]'."
Any suggestions would be greatly appreciated…
Thanks
If you could be less concerned with forcing it into a 2d array, you could just allow the compiler to assign the anonymous IQueryable type and use that for iteration:
Dim ACFTTail5 = (From tailcounts In db.TailCounts _
Where tailcounts.Model = "UH-60A" _
Group tailcounts By _
tailcounts.Tail, tailcounts.Model _
Into g = Group Order By Tail _
Select Tail, rdf = CStr(g.Sum(Function(p) p.TailCount)))
You could then just use the For each
iterator to cycle through whatever you needed.
Edit:
I haven't tested this little piece by any stretch, but you should be able to use a List
Dim ACFTTail5 as List(Of String())
ACFTTail5 = new List(Of String())(From tailcounts In db.TailCounts _
Where tailcounts.Model = "UH-60A" _
Group tailcounts By _
tailcounts.Tail, tailcounts.Model _
Into g = Group Order By Tail _
Select new String() { Tail, rdf = CStr(g.Sum(Function(p) p.TailCount)) })
Dim ACFTTail5_2d_Array as String(,) = ACFTTail5.ToArray()
The key that I'm using here is to select it directly into a new class (in this case a string array), and keep it in a List specifically for string arrays. Then you should be able to send that to a string array of string arrays.
精彩评论