使用linq

时间:2016-04-10 15:00:27

标签: c# linq

我有这些查询从多个表中检索结果并希望得到下面给出的预期结果,这是我的代码:

var q1 = (from sh in SessionHistories
      where (System.DateTime.Now >= sh.FromDate && System.DateTime.Now <= sh.ToDate) && sh.IsDeleted == false
      select sh
).ToList();

var FromDate = (from q in q1 select q.FromDate.Value).FirstOrDefault();
var ToDate = (from q in q1 select q.ToDate.Value).FirstOrDefault();

var allDates = (from idx in Enumerable.Range(0, (System.DateTime.Now -     FromDate).Days)
            select FromDate.AddDays(idx)).OrderBy(x => x.Date).ToList();


var q2 = (from ci in View_checkinout
      where ci.EmployeeId == 2183
      group ci by new { ci.CheckTime.Date } into g
      select new
      {
          AttendanceDate = g.Key.Date,
          EmployeeId = g.Select(x => x.EmployeeId).FirstOrDefault(),
          CheckInTime = g.Select(x => new { x.CheckTime, x.CheckType }).Where(x => x.CheckType == "I").FirstOrDefault(),
          CheckOutTime = g.Select(x => new { x.CheckTime, x.CheckType }).Where(x => x.CheckType == "O").OrderByDescending(x => x.CheckTime).FirstOrDefault()
      })
      .ToList();

var q3 = (from ad in allDates
      join q in q2 on ad.Date equals q.AttendanceDate into outer
      from o1 in outer.DefaultIfEmpty()
      select new
      {
          o1,
          ad
      }).ToList();

这是预期的结果:

| EmployeeName  | Designation  | Late Arrivals| Short Leave| Half Leave| Full Leave| Cur. Month Total| Prev. Total| 
|---------------|--------------|--------------|------------|------------|------------|------------|
|          Name |       Teacher|         0    |          0 |
|          Name |        Admin |        20    |          0 |
|          Name |         Peon |         0    |         30 |

0 个答案:

没有答案