如何将以下sql查询转换为dot概念linq查询?
SELECT f1.device_id, f1.device_model, f2.main_serial_number
FROM Device AS f1
INNER JOIN MainSerialNumber AS f2 ON f1.device_id = f2.device_id
WHERE (f2.main_serial_number IN
(SELECT f2_1.main_serial_number
FROM Device AS f1_1
INNER JOIN MainSerialNumber AS f2_1
ON f1_1.device_id = f2_1.device_id
GROUP BY f2_1.main_serial_number
HAVING (COUNT(f2_1.main_serial_number) > 1)))
答案 0 :(得分:1)
作为查询理解:
var serialNumbers =
from sn in MainSerialNumber
join dev in Device
on sn.DeviceId equals dev.DeviceId
into devices
where devices.Count() > 1
select sn.MainSerialNumber;
var result =
from dev in Device
join sn in MainSerialNumber
on dev.DeviceId equals sn.DeviceId
where serialNumbers.Contains(sn.MainSerialNumber)
select new {
dev.DeviceId,
dev.DeviceModel,
sn.MainSerialNumber
}
使用方法语法(“点符号”):
var serialNumbers = MainSerialNumber
.GroupJoin(Device,
sn => sn.DeviceId,
dev => dev.DeviceId,
(sn, devs) => new
{
Serial = sn.MainSerialNumber,
Count = devs.Count()
}
)
.Where(x => x.Count > 1)
.Select(x => x.Serial);
var result = Device
.Join(MainSerialNumber,
dev => dev.DeviceId,
sn => sn.DeviceId,
(dev, sn) => new
{
dev.DeviceId,
dev.DeviceModel,
sn.MainSerialNumber
}
)
.Where(x => serialNumbers.Contains(x.MainSerialNumber));
对于像这样的查询,查询语法对我来说看起来更清晰。