我正在尝试在实体框架中执行一个在SQL中非常直接的查询,看起来它应该能够处理它,尽管我收到了一个转换错误。
以下声明:
Dim affiliateList = (
From cl In objCxt.CustomerLocations
Join cla In objCxt.CustomerLocation_Affiliation
On cla.CustomerLocationKey Equals cl.CustomerLocationKey
Join cl2 In objCxt.CustomerLocations
On cl2.CustomerLocationKey Equals cla.AffiliateCustomerLocationKey
Where cl.Removed = False _
AndAlso cl.Active = True _
AndAlso cl.CustomerKey = 3 _
AndAlso cl2.Removed = False _
AndAlso cl2.Active = True _
AndAlso hsCustLocCodes.Any(Function(clc) clc.Equals(cl.Code, StringComparison.InvariantCultureIgnoreCase))
).
GroupBy(Function(x) x.cl.Code).
Select(Function(x) New With {.Code = x.Key, .AffiliateCode = x.Max(Function(y) y.cl2.Code)}).
ToList()
产生以下错误:
Unable to cast the type
'System.Linq.IGrouping`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[VB$AnonymousType_73`3[[SmaModelBase.CustomerLocation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[SmaModelBase.CustomerLocation_Affiliation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[SmaModelBase.CustomerLocation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'
to type
'System.Collections.Generic.IEnumerable`1[[VB$AnonymousType_73`3[[SmaModelBase.CustomerLocation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[SmaModelBase.CustomerLocation_Affiliation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[SmaModelBase.CustomerLocation, SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], SmaModelBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'
LINQ to Entities only supports casting EDM primitive or enumeration types.
如果我在SQL中这样做,它将看起来像这样:
select
cl.Code,
AffliateCode = max(cl2.Code)
from
CustomerLocation cl
inner join CustomerLocation_Affiliation cl_a
on cl_a.CustomerLocationKey = cl.CustomerLocationKey
inner join CustomerLocation cl2
on cl2.CustomerLocationKey = cl_a.AffiliateCustomerLocationKey
where
cl.Removed = 0
and
cl.Active = 1
and
cl.CustomerKey = 3
and
cl2.Removed = 0
and
cl2.Active = 1
and
cl.Code in ('123', 'abc')
group by
cl.code
有没有办法完成我在EF尝试做的事情?