Select LINQ 中来自两个 table 的多个字段 Select 使用 LEFT JOIN 查询

Select mutiple fields from two table in LINQ Select Query using LEFT JOIN

我正在尝试使用 LINQ select 所有 CourseInstances (table) 加入课程 table; CourseInstances table 左边是 courses 右边 table。现在我的 LINQ 查询工作正常,直到我在 select new {..... 我收到错误

中添加字段 '_courses.StudyLevelId'

错误

The name '_courses' does not exist in the current context

我正在使用 LINQPad 进行测试....在底部我提供了 ViewModel 方法代码...

LINQ 查询

from _courseInstances in CourseInstances
join _courses in Courses on _courseInstances.CourseID equals _courses.CourseID into c
from a in c.DefaultIfEmpty()
orderby _courseInstances.CourseCode
select new {_courseInstances.CourseID, _courseInstances.CourseCode, _courseInstances.CourseTitle, _courseInstances.UCASCode, _courseInstances.StartDate, _courses.StudyLevelId }

生成的 SQL 脚本没有 _courses.StudyLevelId

SELECT [t0].[CourseID], [t0].[CourseCode], [t0].[CourseTitle], [t0].[UCASCode], [t0].[StartDate]
FROM [CourseInstances] AS [t0]
LEFT OUTER JOIN [Courses] AS [t1] ON [t0].[CourseID] = ([t1].[CourseID])
ORDER BY [t0].[CourseCode]

我创建了 ModelView 来处理单个查询中的多个 table,但我遇到了同样的错误

C# 中的视图模型

 public class CoursesInstanceStudyLevel_ViewModel
{
    public CourseInstanceModel _CourseInstanceModel { get; set; }
    public CoursesModel _CoursesModel { get; set; }
    public StudyLevelModel  _StudyLevelModel { get; set; }
    public ApplicationURLValidityModel  _ApplicationURLValidityModel { get; set; }
}

C# Linq 查询

var bb2 = 
 (from _courseInstances in _uof.CourseInstances_Repository.GetAll()
  join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID into c
  from a in c.DefaultIfEmpty()
  orderby _courseInstances.CourseCode
  select new CoursesInstanceStudyLevel_ViewModel()
        {
          _CourseInstanceModel = _courseInstances,
          _CoursesModel = _courses
        }).ToList();

具有多个连接

我在尝试添加多个连接时遇到以下错误。我相信连接是正确的,与 select{.... 数据为空的位置相同? UWLApplicationURL = _ApplicationURLValidityRules == 空? (bool?)null : _ApplicationURLValidityRules.UWLApplicationURL }

 Error :Cannot assign <null> to anonymous type property
 Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'bool' 

代码

  from _courseInstances in CourseInstances
  join _coursesList in Courses on _courseInstances.CourseID equals _coursesList.CourseID into c
  from _courses in c.DefaultIfEmpty()
  join _ApplicationURLValidityRulesList in ApplicationURLValidityRules on _courseInstances.CourseInstanceID equals _ApplicationURLValidityRulesList.CourseInstanceID into d
  from _ApplicationURLValidityRules in d.DefaultIfEmpty()
  join _studylevel in StudyLevels on _courses.StudyLevelId equals _studylevel.StudyLevelID
  orderby _courseInstances.CourseCode
  select new {_courseInstances.CourseID, _courseInstances.CourseCode, _courseInstances.CourseTitle, _courseInstances.UCASCode, _courseInstances.StartDate, _courses.StudyLevelId, _studylevel.LevelDescription, _studylevel.SLevelType, UWLApplicationURL = _ApplicationURLValidityRules == null? (bool?)null :  _ApplicationURLValidityRules.UWLApplicationURL }

你必须使用 a 而不是 _courses:

 from _courseInstances in _uof.CourseInstances_Repository.GetAll()
  join _courses in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _courses.CourseID into c
  from a in c.DefaultIfEmpty()
  orderby _courseInstances.CourseCode
  select new CoursesInstanceStudyLevel_ViewModel()
        {
          _CourseInstanceModel = _courseInstances,
          _CoursesModel = a
        }).ToList();

我已经设法解决了第一个问题,感谢 Sandeep 让我知道我应该看哪里。接下来我需要检查多个连接

  var bb2 = (from _courseInstances in _uof.CourseInstances_Repository.GetAll()
                     join _coursesList in _uof.Courses_Repository.GetAll() on _courseInstances.CourseID equals _coursesList.CourseID into c
                     from _courses in c.DefaultIfEmpty()
                     orderby _courseInstances.CourseCode
                     select new CoursesInstanceStudyLevel_ViewModel()
                     {
                         _CourseInstanceModel = _courseInstances,
                         _CoursesModel = _courses 
                     }).ToList();