我从下面的查询中收到以下错误;
Unable to create a constant value of type 'tradedata.symbol'. Only primitive types or enumeration types are supported in this context.
注意:查询被分解为两个,因为我无法查询两个单独的上下文,并且我无法将symbolIdList连接到LINQ中的投影列表作为其在memeory中的对象,因此我将projList放在内存中,因此两个对象都将在内存中。
Public Function FillRangeProjDropdown(ByRef ddl As DropDownList, includeEquities As Boolean) As DropDownList
Dim requestedDate As Date = MySqlDate(DateTime.Now.Date)
Dim symbolTypeList As New List(Of Integer)(New Integer() {17, 18, 19})
Dim symbolIdList As List(Of symbol)
Using symbCtx As New SymbolsEntities()
If Not includeEquities Then
symbolIdList = (From d In symbCtx.symbols
Where symbolTypeList.Contains(d.SymbolType)
Select d).ToList()
Else
symbolIdList = (From d In symbCtx.symbols
Where d.SymbolType = 20
Select d).ToList()
End If
End Using
Using projCtx As New ProjectionsEntities()
Dim dvpList As New List(Of DataValuePair)
Dim projList As List(Of projection) = (From d In projCtx.projections
Where d.Date = requestedDate
Select d).ToList()
'symbolIdList is a collection of objects in memory and you cannot join a set of data in the database with another set of data that is in memory.
dvpList = (From d In projList
Join e In symbolIdList On e.Id Equals d.SymbolId
Select New DataValuePair() With {
.Text = e.Name,
.Value = e.Id}).Distinct().OrderBy(Function(o) o.Text).ToList()
For x As Integer = 1 To dvpList.Count
ddl.Items.Add(New ListItem(dvpList(x - 1).Text, dvpList(x - 1).Value))
Next
ddl.Items.Insert(0, New ListItem("Select a Commodity", 0))
End Using
Return ddl
End Function
答案 0 :(得分:0)
可能是这部分:
Where symbolTypeList.Contains(d.SymbolType)
我不知道Entity Framework的最新版本,但是版本达到4.0并且包括4.0在处理Contains
子句方面都是出了名的。我有a similar problem myself。如果您只有3个值进行比较,请尝试明确写出来:
Where d.SymbolType = 17
Or d.SymbolType = 18
Or d.SymbolType = 19
我的代码中没有其他任何内容可能会导致您发布的错误消息。
答案 1 :(得分:0)
由于您需要在内存中同时具有上下文变量,因为当另一个变量在内存中时无法查询数据库对象,因此在当前迭代中,不需要.Distinct(),因此删除.Distinct()可以解决问题
更新了代码
Public Function FillRangeProjDropdown(ByRef ddl As DropDownList, includeEquities As Boolean) As DropDownList
Dim requestedDate As Date = MySqlDate(DateTime.Now.Date)
Dim symbolTypeList As New List(Of Integer)(New Integer() {17, 18, 19})
Dim symbolIdList As List(Of symbol)
Using symbCtx As New SymbolsEntities()
If Not includeEquities Then
symbolIdList = (From d In symbCtx.symbols
Where symbolTypeList.Contains(d.SymbolType)
Select d).ToList()
Else
symbolIdList = (From d In symbCtx.symbols
Where d.SymbolType = 20
Select d).ToList()
End If
End Using
Using projCtx As New ProjectionsEntities()
Dim projList As List(Of projection) = (From d In projCtx.projections
Where d.Date = requestedDate AndAlso d.ProjectionTypeId = 1
Select d).ToList()
' Since symbolIdList is a collection of objects in memory and you cannot join a set of data
' in the database with another set of data that is in memory, projList is set up in memory as well
Dim dvpList As List(Of DataValuePair) = (From d In projList
Join e In symbolIdList On e.Id Equals d.SymbolId
Select New DataValuePair() With {
.Text = e.Name,
.Value = e.Id}).OrderBy(Function(o) o.Text).ToList()
'no .Distinct() needed here as we are doing that when the two objects in memory are created
For x As Integer = 1 To dvpList.Count
ddl.Items.Add(New ListItem(dvpList(x - 1).Text, dvpList(x - 1).Value))
Next
ddl.Items.Insert(0, New ListItem("Select a Commodity", 0))
End Using
Return ddl
End Function