var q = (from Labels in dc.tblArtworkDataLabels select Labels).ToList();
但我需要这样做相当于:
SELECT d.ID, d.labelID, d.dataID, d.data, l.templateID
FROM tblArtworkDataLabels AS d INNER JOIN
tblArtworkData AS l ON d.dataID = l.ID
WHERE (l.templateID = 238)
如何在LINQ中执行此操作?
抱歉!错过了原始陈述中的WHERE子句!
答案 0 :(得分:3)
var result = dc.tblArtworkDataLabels
.Join(dc.tblArtworkData, l => l.ID, d => d.dataID, (l, d) => new {l, d})
.Select(o => new {
Id = o.d.ID,
LabelId = o.d.labelID,
DataId = o.d.dataID,
Data = o.d.data,
TemplateId = o.l.templateID,
})
.Where(o => o.l.templateID == 238);
答案 1 :(得分:2)
如果tblArtworkData
上的主键tblArtworkDataLabels
上有正确的外键,并且已将它们正确导入DBML设计器,则可以让LINQ2SQL隐式创建连接:
from l in tblArtworkData
where l.templateID = 238
select new {
Id = l.tblArtworkDataLabel.ID,
LabelId = l.tblArtworkDataLabel.labelID,
DataId = l.tblArtworkDataLabel.dataID,
Data = l.tblArtworkDataLabel.data,
TemplateId = l.templateID,
}
请参阅my answer关于“LINQ to SQL:多个连接多个列的问题。这可能吗?”关于隐式连接如何转换为SQL。
如果我误解了您的关系并且您有很多tblArtworkDataLabels
到一个tblArtworkData
,则必须以相反的方式转换查询
from d in tblArtworkDataLabels
where d.tblArtworkData.templateID = 238
select new {
Id = d.ID,
LabelId = d.labelID,
DataId = d.dataID,
Data = d.data,
TemplateId = d.tblArtworkData.templateID,
}
答案 2 :(得分:1)
试
var q = (from Labels in dc.tblArtworkDataLabels
join data in dc.tblArtworkData on Labels.ID equals data.DataID select Labels).ToList();