我有一个复杂的LINQ to SQL查询,它连接到两个表 - 一个很简单,工作正常,但一个相当复杂,我得到The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin
这是一个相当长的查询,我在一个有互联网访问权限的工作上进行开发,所以我想我会看看这个问题似乎就足够了:
join consignments in dc.Consignments
.FirstOrDefault(x => x.TripDate > dateFrom
&& x.TripDate < dateTo
&& x.DeliveryDepot == depot.Letter
&& (x.DeliveryStatus == 2 || x.DeliveryStatus == 3))
on new { Reg = s.VehicleReg, Depot = s.VehicleDepot }
equals new { Reg = consignments.VehicleReg, Depot = consignments.DeliveryDepot }
into con
我确保数据类型相同,但仍然无法正常工作。有什么想法吗?
答案 0 :(得分:18)
您确定s.VehiculeDepot与consignments.DeliveryDepot的类型相同吗?
on new { Reg = s.VehicleReg, Depot = s.VehicleDepot }
equals new { Reg = consignments.VehicleReg, Depot = consignments.DeliveryDepot }
答案 1 :(得分:4)
我认为问题出在这一部分:
on new { Reg = s.VehicleReg, Depot = s.VehicleDepot }
equals
new { Reg = consignments.VehicleReg, Depot = consignments.DeliveryDepot }
确保s.VehicleReg
与consignments.VehicleReg
的类型相同,s.VehicleDepot
的类型与consignments.DeliveryDepot
相同。
答案 2 :(得分:0)
还要确保您的成员名称完全相同且相同。例如:
new { Reg = s.VehicleReg, Depot = s.VehicleDepot }
equals
new { Reg = consignments.VehicleReg, DEpot = consignments.DeliveryDepot }
会失败,因为在第二个匿名类型中,depot是不同的情况。