SELECT Meter.SerialNumber as serial,
SupplyPoint.Id as supplypoint,
SupplyType.Id as supplytype,
SupplyType.Name as supplytypename
FROM Meter
INNER JOIN SupplyPoint ON Meter.SupplyPointId = SupplyPoint.Id
INNER JOIN SupplyType ON SupplyPoint.SupplyTypeId = SupplyType.Id;
我有这个查询,以便我可以根据它的序列找到仪表的电源类型。到目前为止,我已经写了这个函数:使用LINQ查询SQL数据库
var query = from meters in db.Meters
join supplyPoint in db.SupplyPoints on meters.SupplyPointId
equals supplyPoint.Id
join supplyType in db.SupplyTypes on supplyPoint.SupplyTypeId equals
supplyType.Id
select new { serial = meters.SerialNumber, type = supplyType.Name };
foreach (var meter in query)
{
if (meter.serial == serial)
return meter.type;
}
return "Meter Type Not Specified";`
所以我调用FindType(string serial)并返回类型。任何人都可以提出更好的代码转换这样的查询?任何有关LINQ的更多信息的方向也欢迎。
你到目前为止尝试过什么?你的数据库表是什么样的?你有没有使用实体框架设置数据上下文? – slugster
你的转换看起来不错,但通常你会用LINQ来找到你的答案:'var ans =(从meter中查询meter.serial ==串行选择计量器).SingleOrDefault();返回(米==空)? “未指定仪表类型”:meter.type;' – NetMage