我必须在LINQ培训中遗漏一些东西。在使用c#的Linq to SQL中,我想查询两个表,一个用外键到另一个表,并传递两个参数,每个表一个。
在SQL中,它就像
一样简单Select Value from Table1 T1
INNER JOIN Table2 T2
On T1.DefID = T2.ID
Where T1.PollID = 1
AND T2.Name = 'Question'
在LINQ中,我被卡住了!
var q = from t1 in dc.Table1
join t2 in dc.Table2
on t1.DefID equals t2.ID
into newGroup
where t1.PollID == 1 // here's where I get stuck!
// how do I query t2?
select newGroup;
我的方法有误吗?提前谢谢。
答案 0 :(得分:2)
var q = from t1 in dc.Table1
join t2 in dc.Table2
on t1.DefID equals t2.ID
where t1.PollID == 1 &&
t2.Name == "Question"
select new {
Alias1 = t1.FieldName,
Alias2 = t2.FieldName
};