EF Core 3.0 - System.Linq.Dynami c .Core - Обновите Linq для работы в 3.0 после обновления - PullRequest
0 голосов
/ 24 января 2020

У меня есть запрос linq, который больше не работает (с момента обновления до ядра 3.0). Если я закомментирую строки «если», это сработает.

Если я просто наберу .OrderBy("FromDate"), он сломается, потому что создает следующее

(ti => ti.Outer.FromDate.HasValue == True ? ti.Outer.FromDate.GetValueOrDefault().ToString("MM/dd/yyyy") : "")

и говорит, что не может перевести его.

У меня есть параметр поиска и имя столбца для сортировки. Как я могу обойти эти проблемы, чтобы заставить его работать в 3.0?

  public IQueryable<TimeSheetMasterView> ShowMyApprovedTimeSheet(string sortColumn, string sortColumnDir, string Search, string UserID)
    {
        var iQueryabletimesheet = (from timesheetmaster in _context.Set<TimeSheetMaster>()
                                   join registration in _context.Set<AspNetUsers>() on timesheetmaster.UserId equals registration.Id
                                   where timesheetmaster.TimeSheetStatus == 2 && timesheetmaster.UserId == UserID
                                   select new TimeSheetMasterView
                                   {
                                       TimeSheetStatus = "Approved",
                                       Comment = timesheetmaster.Comment,
                                       TimeSheetMasterID = timesheetmaster.TimeSheetMasterId,
                                       FromDate = timesheetmaster.FromDate.HasValue == true ? timesheetmaster.FromDate.GetValueOrDefault().ToString("MM/dd/yyyy") : "",
                                       ToDate = timesheetmaster.ToDate.HasValue == true ? timesheetmaster.ToDate.GetValueOrDefault().ToString("MM/dd/yyyy") : "",
                                       CreatedOn = timesheetmaster.CreatedOn.HasValue == true ? timesheetmaster.CreatedOn.GetValueOrDefault().ToString("MM/dd/yyyy") : "",
                                       TotalHours = timesheetmaster.TotalHours,
                                       Username = registration.Email,
                                       SubmittedMonth = timesheetmaster.ToDate.HasValue == true ? timesheetmaster.ToDate.GetValueOrDefault().ToString("MMM") : "",
                                   }); 


        if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
        {
            if (sortColumnDir == "asc")
            {
                iQueryabletimesheet = iQueryabletimesheet.OrderBy(s => s.GetType().GetProperty(sortColumn).GetValue(s, null)).ThenBy(o => o.Username).ThenBy(o => o.FromDate);
            }
            else
            {
                iQueryabletimesheet = iQueryabletimesheet.OrderByDescending(s => s.GetType().GetProperty(sortColumn).GetValue(s, null)).ThenBy(o => o.Username).ThenBy(o => o.FromDate);
            }
        }

        if (!string.IsNullOrEmpty(Search))
        {
            iQueryabletimesheet = iQueryabletimesheet.Where(m => EF.Functions.Like(m.Username, $"%{Search}%") || EF.Functions.Like(m.SubmittedMonth, $"%{Search}%") || EF.Functions.Like(m.TimeSheetStatus, $"%{Search}%") || m.ToDate.Contains(Search) || m.FromDate.Contains(Search));
        }
        return iQueryabletimesheet;
    } 

Спасибо за вашу помощь!

...