EF实现左连接时,一次只能实现一次左联。有时候会出现需要右连接的问题。此时,我的做法是在中间建立一张过渡的虚拟表。
var subQuery = from tt in deviceInfo
join t7 in deviceType on tt.deviceTypeId equals t7.id
into f
from fq in f.DefaultIfEmpty()
join t2 in distritCode on tt.provinceId equals t2.id
into b
from bq in b.DefaultIfEmpty()
join t3 in distritCode on tt.cityId equals t3.id
into c
from cq in c.DefaultIfEmpty()
join t4 in distritCode on tt.countyId equals t4.id
into d
from dq in d.DefaultIfEmpty()
select new DeviceParameter
{
id = tt.id,//设备Id号
indexCode = tt.indexCode,
deviceType = fq.chnName,
province = bq.chnName,
city = cq.chnName,
county = dq.chnName
};
var query = from t1 in alarmCase
join t2 in subQuery on t1.alarmId equals t2.id
into a
from aq in a.DefaultIfEmpty()
join t6 in deviceInfo on t1.robotId equals t6.id
into e
from eq in e.DefaultIfEmpty()
select new AlarmParameter
{
id = t1.id,
alarmIndexCode = t1.indexCode,
startTime = t1.startTime,
caseType = t1.caseType,
deviceId = t1.alarmId,
deviceType = aq.deviceType,
deviceIndexCode = aq.indexCode,
province = aq.province,
city = aq.city,
county = aq.county,
duration = t1.duration,
robotIndexCode = eq.indexCode, //机器人设备编号
status = t1.status
};
我们最终需要查询的内容是query,而subQuery是中间的虚拟表。有更简洁的做法请大佬指教,谢谢


版权声明:本文为qq_34801230原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_34801230/article/details/94486957