Sandeep Kumar

Sandeep Kumar

  • 1.1k
  • 683
  • 60.1k

how to set order by desc on two columns with different condion

Apr 3 2023 8:23 AM

I want to filter data when Modifyon is null then on basis of createon orderby  and when Modifyon is not null then orderby on modifyon

async Task<PagedList<NotifyData>> IManager.GetApprovedUnapprovednotificationList(NotificationPagingSearch Items)
{
    List<NotifyData> _NotifyData = new List<NotifyData>();
    var DeptId = _authenticationManager.GetAuthenticatedUser().Result.DeptId;
    try
    {

        _NotifyData = (from Notification in _dbContext.NotificationHeader
                       join MstStage in _dbContext.MstStage on Notification.Status equals MstStage.Id
                       where Notification.DeptId == (DeptId == 0 ? Notification.DeptId : DeptId)
                       && Notification.Status == (Items.Status == 100 ? Notification.Status : Items.Status)
                       && Notification.CreatedOn.Value.Date >= (Items.FromDate == string.Empty ? Notification.CreatedOn.Value.Date : ApplicationData.AppStringToDatetime(Items.FromDate))
                       && Notification.CreatedOn.Value.Date <= (Items.ToDate == string.Empty ? Notification.CreatedOn.Value.Date : ApplicationData.AppStringToDatetime(Items.ToDate))
                       select new
                       {
                           RefNId = Notification.RefNid,
                           NType = Notification.Ntype,
                           UserType = (int)Notification.UserType,
                           DeptId = (int)Notification.DeptId,
                           CourseId = (int)Notification.CourseId,
                           NotificationH = Notification.NotificationH,
                           NotificationB = Notification.NotificationB,
                           Comment = Notification.Comment,
                           StatusId = (int)MstStage.Id,
                           Status = MstStage.Mode,
                           CreatedBy = (int)Notification.CreatedBy,
                           CreatedOn = Notification.CreatedOn,
                           ModifiedBy = (int)Notification.ModifiedBy,
                           ModifiedOn = ApplicationData.AppDateToString(Notification.ModifiedOn)
                       }).AsEnumerable().OrderByDescending(x => x.CreatedOn).Select((k, index) => new NotifyData()
                       {
                           SrlNo = index + 1,
                           RefNId = k.RefNId,
                           NType = k.NType,
                           RoleName = GetUserTypeName(k.UserType).Result,
                           DeptId = k.DeptId,
                           DeptName = GetDepartmentName(k.DeptId).Result,
                           CourseId = k.CourseId,
                           CourseName = GetCourseName(k.CourseId).Result,
                           NotificationH = k.NotificationH,
                           NotificationB = k.NotificationB,
                           Comment = k.Comment,
                           StatusId = k.StatusId,
                           Status = k.Status,
                           CreatedBy = k.CreatedBy,
                           CreatedOn = ApplicationData.AppDateToString(k.CreatedOn),
                           ModifiedBy = k.ModifiedBy,
                           ModifiedOn = k.ModifiedOn,
                           Can_Approve = GetRegistationBtnAccessResult(_authenticationManager.GetAuthenticatedUser().Result.UserId, UserManageAccess.NotificationPage, UserManageAccess.NotificationApproveAction, k.RefNId).Result,
                           //   Can_Save = GetRegistationResult(_authenticationManager.GetAuthenticatedUser().Result.UserId, UserManageAccess.NotificationPage, UserManageAccess.NotificationSendAction).Result

                       }).ToList();


    }
    catch (Exception ex)
    {

    }

    return await Task.Run(() =>
    { 
        return new PagedList<NotifyData>(_NotifyData.AsQueryable(), Items.PageNumber, Items.PageSize);
    });
}

 


Answers (2)