我正在尝试从datacontext获取数据。通常,我没有遇到任何问题,但是我在尝试返回键/值对列表时遇到了问题。
基本上,我尝试从表中获取所有唯一的名称作为键列,以及它们在表中出现的次数作为值列。
我的数据将如下所示:
apple 5
banana 1
dragonfruit 3
.
.
.
完整的错误消息是:
无法将'System.Data.Linq.DataQuery
1[VB$AnonymousType_3
2System.String,System.Int32]‘类型的对象强制转换为'System.Collections.Generic.List`1’类型
我使用的代码是这样的:
Dim indicators As List(Of Object)
Public Sub GetIndicatorData()
Using context = new A_DataContext
indicators = (From p In chartdata Group p By __groupByKey1__ = p.INDK8R Into g = Group
Select New With {.name = __groupByKey1__, .count = g.Count()}).AsEnumerable()
indDataSource = indicators
End Sub
但我也尝试过:
发布于 2019-04-23 03:32:15
使用类来封装您的匿名类型
Public Class NameAndCount
Public ReadOnly Property Name As String
Public ReadOnly Property Count as Integer
Public Sub New(name As String, count As Integer)
Me.Name = name
Me.Count = count
End Sub
End Class
' ...
Private indicators As IEnumerable(Of NameAndCount)
Public Sub GetIndicatorData()
Using context = new A_DataContext
indicators = From p In chartdata Group p By __groupByKey1__ = p.INDK8R Into g = Group
Select New NameAndCount(__groupByKey1__, g.Count())
indDataSource = indicators.ToList()
End Using
End Sub
发布于 2019-04-22 21:33:06
我想通了。我将指示器的声明简单地更改为一个对象,然后从查询结果中删除了.enumerable (或.toList)。
感谢您的考虑和时间。
https://stackoverflow.com/questions/55795077
复制相似问题