为什么我的SQL结果与Linq结果不匹配?

时间:2015-08-27 17:05:01

标签: c# sql linq linq-to-sql

当我在SQL中运行此查询时,我得到了我想要的内容:

SELECT Auckland_Park.Formative.[Formative Name]
FROM Auckland_Park.LearningUnit 
INNER JOIN Auckland_Park.Formative 
ON Auckland_Park.LearningUnit.ID = Auckland_Park.Formative.FK_LU 
INNER JOIN Auckland_Park.Reference 
INNER JOIN Auckland_Park.Course 
ON Auckland_Park.Reference.FK_Course = Auckland_Park.Course.ID 
ON Auckland_Park.LearningUnit.ID = Auckland_Park.Reference.FK_LU
WHERE Auckland_Park.Course.Name = 'BI'

我的结果:

Querying SQL Build 
Report Develop 
Java App Develop 
Andriod App Set up
SharePoint Server

但是当我使用我的C#应用​​程序时,我正在使用LINQ to SQL,我的LINQ查询看起来像这样:

//LINQ Query to fill Foramtive Name ComboBox
CTUDataContext data = new CTUDataContext();
var course = (from r in data.LearningUnits                      
              join a in data.Formatives
              on r.ID equals a.FK_LU
              join f in data.References
              on r.ID equals f.FK_LU
              join g in data.Courses 
              on f.FK_LU equals g.ID
              where g.Name == ("BI")
              select new
              {
                  formativeName = a.Formative_Name,
                  ID = a.ID

              } 
              ).ToList();
txtFormativeName.ItemsSource = course;
txtFormativeName.DisplayMemberPath = "formativeName";
txtFormativeName.SelectedValuePath = "ID";

看起来是一样的,但是我得到的结果与上面的SQL查询不一样。

1 个答案:

答案 0 :(得分:0)

ArrayList

解答:

    //LINQ Query to fill Foramtive Name ComboBox
CTUDataContext data = new CTUDataContext();
var course = (from r in data.LearningUnits                      
              join a in data.Formatives
              on r.ID equals a.FK_LU
              join f in data.References
              on r.ID equals f.FK_LU
              join g in data.Courses 
              on f.FK_LU equals g.ID
              where g.Name == ("BI")
              select new
              {
                  formativeName = a.Formative_Name,
                  ID = a.ID

              } 
              ).ToList();
txtFormativeName.ItemsSource = course;
txtFormativeName.DisplayMemberPath = "formativeName";
txtFormativeName.SelectedValuePath = "ID";

必须替换为

on f.FK_LU equals g.ID


f.FK_LU