我想使用join语句将sql查询更改为linq。查询应检索列(days
和startdate
),同时将表1的记录与表匹配。简而言之,使用join语句将sql查询转换为linq。
以下是我的尝试。
SQL查询(工作)
SELECT *
FROM dbo."Batches"
INNER JOIN dbo.StudentBatchRelation
on dbo.Batches.Id = dbo.StudentBatchRelation.BatchId
WHERE
dbo.StudentBatchRelation.StudentId = '3d980306-e36e-4581-8c98-219717cb1668'
LINQ(不提取结果)
var result = (from t1 in contBatch.GetallBatchList()
join t2 in contStudent.getAllStudentBatchList()
on t1.Id equals t2.batchId where t2.studentId == studentid
select new { t1.Days, t1.BatchDate }).ToList();
答案 0 :(得分:1)
如果您的EF实体定义明确,您可以使用以下方法简化查询:
var result = Db.Batches
.Include(p => p.StudentBatchRelation)
.Where(p => p.StudentBatchRelation.StudentId = "3d980306-e36e-4581-8c98-219717cb1668")
.ToList();
否则,如果您必须使用Getallxxxx
功能,则可以执行以下操作:
var result = (from t1 in contBatch.GetallBatchList()
join t2 in contStudent.getAllStudentBatchList()
on t1.Id equals t2.batchId
where t2.studentId == "3d980306-e36e-4581-8c98-219717cb1668"
select t1)
.ToList();