如何将linq中的多个表连接到sql

时间:2017-05-16 11:07:36

标签: c# sql-server linq

var data = from c1 in db.CCEScholasticTests.Where(x=> (x.CCEvaluationID == (CCEValuationID == 0 ? x.CCEvaluationID: CCEValuationID) && x.SubjectID == SubjectID && x.languageTypeSubjectID==languageTypeSubjectID && (x.BranchID == 0 || x.BranchID==BranchID) && x.BranchSectionID == BranchSectionID))
                       join ce in db.CCEvaluations on c1.CCEvaluationID equals ce.CCEvaluationID
                       join ct in db.CCETerms on new {key1 =ce.AcademicPlaningTermID, key2=ce.CCEStructureID, key3=BranchID, key4=c1.AcademicYearID} equals new {key1 = ct.AcademicPlaningTermID, key2=ct.CCEStructureID, key3=ct.BranchID, key4=ct.AcademicYearID}

我正在使用三张桌子。我不太了解LINQ。请帮我在三张桌子上写一个连接。

2 个答案:

答案 0 :(得分:1)

如果您只是简单地使用linq连接语法,请在最基本的连接示例之下:

 var joinQuery = from c in customers
                 join p in purchases on c.ID equals p.CustomerID
                 select new { c.Name, p.Description, p.Price }

 //fluent syntax; though query syntax is usually preferred.
 var fluentQuery = customers.Join(purchases, c => c.ID, p => p.CustomerID,
                       (c, p) => new { c.Name, p.Description, p.Price });

多连接遵循相同的逻辑。

答案 1 :(得分:0)

LINQ Join with Multiple From Clauses

看看这个问题。 您可以在同一查询中使用多个from