LINQ EF加入CROSS JOIN

时间:2011-11-01 19:45:33

标签: linq entity-framework join

这个linq到ef语法产生如下所示的sql语法。如何在没有CROSS JOIN的情况下生成它?交叉连接给了我很多额外的记录。

vehicleList = (from _vehicle in shireyContext.Vehicles
                                   join _statusDescription in shireyContext.StatusDescriptions
                                   on _vehicle.Status equals _statusDescription.StatusId
                                   join _newOptions2 in shireyContext.VehicleOption_New
                                   on _vehicle.StockNo equals _newOptions2.StockNo
                                   where _vehicle.NewOrUsed == NewOrUsed && _vehicle.Model == Model && _newOptions2.Color != null                                       
                                   from _newOptions in shireyContext.VehicleOption_New
                                   select new VehicleDomainEntity
                                   {
                                       StockNo = _vehicle.StockNo,
                                       Year = _vehicle.VehicleYear,
                                       Make = _vehicle.Make,
                                       Model = _vehicle.Model,
                                       Description = _newOptions2.Description,
                                       ExteriorColor = _vehicle.ExteriorColor,
                                       InteriorColor = _vehicle.InteriorColor,
                                       InternetPrice = _vehicle.CodedCost,
                                       ListPrice = _vehicle.ListPrice,
                                       Status = _statusDescription.StatusDescriptionText,
                                       NewOrUsed = _vehicle.NewOrUsed,
                                       Mileage = _vehicle.Mileage,
                                       VIN = _vehicle.VIN
                                   }).ToList();

生成此sql:

 SELECT
Extent2.StatusId AS StatusId,
Extent1.StockNo AS StockNo,
Extent1.VehicleYear AS VehicleYear,
Extent1.Make AS Make,
Extent1.Model AS Model,
Extent3.Description AS Description,
Extent1.ExteriorColor AS ExteriorColor,
Extent1.InteriorColor AS InteriorColor,
Extent1.CodedCost AS CodedCost,
Extent1.ListPrice AS ListPrice,
Extent2.StatusDescriptionText AS StatusDescriptionText,
Extent1.NewOrUsed AS NewOrUsed,
Extent1.Mileage AS Mileage,
Extent1.VIN AS VIN
FROM  dbo.Vehicles AS Extent1
INNER JOIN dbo.StatusDescription AS Extent2 ON Extent1.Status = Extent2.StatusId
INNER JOIN dbo.VehicleOption_New AS Extent3 ON Extent1.StockNo = Extent3.StockNo
CROSS JOIN dbo.VehicleOption_New AS Extent4
WHERE (Extent1.NewOrUsed = 'N') AND (Extent1.Model = 'cts' AND (Extent3.Color IS NOT NULL))

1 个答案:

答案 0 :(得分:0)

我想你想要这个

from _vehicle in shireyContext.Vehicles
join _statusDescription in shireyContext.StatusDescriptions
on _vehicle.Status equals _statusDescription.StatusId
join _newOptions2 in shireyContext.VehicleOption_New into VehicleNew
on _vehicle.StockNo equals _newOptions2.StockNo
where _vehicle.NewOrUsed == NewOrUsed && _vehicle.Model == Model && _newOptions2.Color != null
from _newOptions in VehicleNew

更改的两行是:

 join _newOptions2 in shireyContext.VehicleOption_New into VehicleNew

from _newOptions in VehicleNew