使用where子句在Linq查询上使用左连接的语法错误

时间:2009-10-27 09:21:42

标签: linq-to-sql

我希望这很容易被发现。 此查询有一个synatx错误;

public static IEnumerable<DailyTimeRecorded> GetPeriodData(
            Employee emp, DateTime startDate, DateTime endDate)
        {
            var listDTR =
                from dow in db.DayOfTheWeeks
                join dtr in db.DailyTimeRecordeds
                on dow.DayOfTheWeekId equals dtr.DayOfTheWeekId into sr
                where dtr.EmployeeId == emp.EmployeeId
                from x in sr.DefaultIfEmpty()
                select new
                {
                    DailyTimeRecordedId = x.DailyTimeRecordedId == null ? -1 : x.DailyTimeRecordedId,
                    DayOfTheWeekId = dow.DayOfTheWeekId,
                    EmployeeId = emp.EmployeeId,
                    MorningTimeIn_HH = x.MorningTimeIn_HH == null ? 0 : x.MorningTimeIn_HH,
                    MorningTimeIn_MM = x.MorningTimeIn_MM == null ? 0 : x.MorningTimeIn_MM
                 };
          return listDTR  as IEnumerable<DailyTimeRecorded>;
      }

语法错误就行了;其中dtr.EmployeeId == emp.EmployeeId 我得到的错误是; “当前上下文中不存在名称'dtr'”

1 个答案:

答案 0 :(得分:1)

这是解决方案!

public static IEnumerable<DailyTimeRecorded> GetPeriodData(
            Employee emp, DateTime startDate, DateTime endDate)
        {
            var listDTR =
                from dow in db.DayOfTheWeeks
                join dtr in db.DailyTimeRecordeds
                on dow.DayOfTheWeekId equals dtr.DayOfTheWeekId into sr
                from x in sr.DefaultIfEmpty()
                where x.EmployeeId == emp.EmployeeId
                select new
                {
                    DailyTimeRecordedId = x.DailyTimeRecordedId == null ? -1 : x.DailyTimeRecordedId,
                    DayOfTheWeekId = dow.DayOfTheWeekId,
                    EmployeeId = emp.EmployeeId,
                    MorningTimeIn_HH = x.MorningTimeIn_HH == null ? 0 : x.MorningTimeIn_HH,
                    MorningTimeIn_MM = x.MorningTimeIn_MM == null ? 0 : x.MorningTimeIn_MM
                 };
          return listDTR  as IEnumerable<DailyTimeRecorded>;
      }