C#
var _NotifyData = (from Notification in _dbContext.Notification
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 >= (Items.FromDate == string.Empty ? Notification.CreatedOn : ApplicationData.AppStringToDatetime(Items.FromDate)) && Notification.CreatedOn new NotifyData()
{
SrlNo = index + 1,
RefNId = k.RefNId,
NType = k.NType,
NotificationH = k.NotificationH,
NotificationB = k.NotificationB,
Comment = k.Comment,
ApproveBy = k.ApproveBy,
StatusId = k.StatusId,
Status = k.Status,
CreatedBy = k.CreatedBy,
CreatedOn = k.CreatedOn,
ModifiedBy = k.ModifiedBy,
ModifiedOn = k.ModifiedOn,
Can_Approve = GetRegistationResult(_authenticationManager.GetAuthenticatedUser().Result.UserId, UserManageAccess.NotificationPage, UserManageAccess.NotificationApproveAction).Result,
}).ToList();
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 24, 2023, 6:17 AM EDT
In the SQL query, the
CreatedOnfield is implicitly converted to adatetimedata type, whereas in the LINQ query, theCreatedOnfield is being converted to a string using theAppDateToStringmethod. If theCreatedOnfield in theNotificationentity is not already adatetimedata type, it's possible that the conversion to a string is affecting the ordering of the results.Thanks
Sandeep KumarRank #1,642 · 937 reputationPosted Mar 24, 2023, 6:25 AM EDT
This is My Table Class .model of tables
public partial class Notification { public int Id { get; set; } public string RefNid { get; set; } public string Ntype { get; set; } public int? RoleId { get; set; } public int? DeptId { get; set; } public int? CourseId { get; set; } public int? UserId { get; set; } public string NotificationH { get; set; } public string NotificationB { get; set; } public string Comment { get; set; } public int? Status { get; set; } public int? ApproveBy { get; set; } public int CreatedBy { get; set; } public DateTime? CreatedOn { get; set; } public int? ModifiedBy { get; set; } public DateTime? ModifiedOn { get; set; } public int? ReadTag { get; set; } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 26, 2023, 5:34 AM EDT
The CreatedOn field in the Notification table has the correct data type (i.e., datetime, datetime2, or datetimeoffset). If the data type is different, it may not sort the results as expected.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 26, 2023, 5:35 AM EDT
If the CreatedOn field contains null values, they will be treated as the smallest value and will be sorted first, regardless of the order by clause. You can add a where Notification.CreatedOn is not null condition to exclude null values from the result set. Also if you have implemented a custom sorting logic using a SQL trigger or stored procedure, it may override the order by clause and return the results in a different order.