【问题标题】:LINQ EF Join with added CROSS JOINLINQ EF 加入并添加了 CROSS JOIN
【发布时间】:2011-11-01 19:45:33
【问题描述】:

此 linq to 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))

【问题讨论】:

    标签: linq entity-framework join


    【解决方案1】:

    我想你想要这个

    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
    

    【讨论】:

    • 我的错!有一个错字。请注意,我加入 newOptions2,然后从 newOptions 中进行选择。最后一个 from 不应该在那里。感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2021-01-07
    • 2015-03-23
    相关资源
    最近更新 更多