为什么我的 SQL 结果与我的 Linq 结果不匹配?
Why don't my SQL results match my Linq results?
当我在 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 查询得到的结果不同。
//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
必须替换为
f.FK_Course
当我在 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 查询得到的结果不同。
//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
必须替换为
f.FK_Course