使用LEFT JOIN在LINQ Select Query中从两个表中选择多个字段

时间:2015-01-22 11:11:26

标签: sql-server asp.net-mvc linq entity-framework

我正在尝试使用LINQ选择所有CourseInstances(表)左连接到Courses表; CourseInstances表留在课程右表的位置。现在我的LINQ查询工作正常,直到我添加字段' _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来处理单个查询中的多个表,但是我得到了相同的错误

C#中的ViewModel

 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 {.... where data is null ?? UWLApplicationURL = _ApplicationURLValidityRules == null? (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 }

2 个答案:

答案 0 :(得分:1)

你必须使用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();

答案 1 :(得分:0)

我已经设法解决了第一个问题,感谢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();