我在SQL Management Studio中编写的以下查询,它工作正常
select distinct location.LocationId, location.Name, location.WorkArea,
crop.CropId, HubCropTypes.Name, crop.recommendedDate, activity.ApplicationDate, activity.Type, crop.IsDeleted cropDeleted, activity.IsDeleted activityDeleted
from location
left outer join crop on location.LocationId = crop.LocationId
left outer join activity on crop.CropId = Activity.CropID
left outer join HubCropTypes on crop.HubCropTypeID = HubCropTypes.HubCropTypeID
where location.FarmId = 'xxxxxxxxx'
and location.IsDeleted = 0
order by location.LocationId,crop.recommendedDate, location.Name
然而,LINQ版本没有,并且在SQL中玩了之后我发现它是左边的连接。我以为DefaultIfEmpty替换了LINQ中的左连接?
var query = (from l in _db.Locations
join c in _db.Crops.DefaultIfEmpty() on l.LocationId equals c.LocationId
join a in _db.Activities.DefaultIfEmpty() on c.CropId equals a.CropID
join h in _db.HubCropTypes.DefaultIfEmpty() on c.HubCropTypeID equals h.HubCropTypeID
where l.FarmId == FarmID
where l.IsDeleted == false
select new
{
LocationID = l.LocationId,
LocationName = l.Name,
WorkArea = l.WorkArea,
CropID = c.CropId,
CropName = h.Name,
CropRecommendedDate = c.RecommendedDate,
ActivityApplicationDate = a.ApplicationDate,
ActivityType = a.Type,
isCropDeleted = c.IsDeleted,
isActivityDeleted = a.IsDeleted
}).Distinct();