[SOLVED] Convert Gregorian date to Hijri date In LINQ sql – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Abdullah Al-Mohammadi

I used c# asp.net mvc5 …

How I Covert Gregorian date to Hijri date In LINQ sql ?

         CultureInfo arSA = new CultureInfo("ar-SA");
        arSA.DateTimeFormat.Calendar = new HijriCalendar(); 

   ViewBag.Service_Get_REC = new List<svr_Service_Get_REC> 
   ((from m in _db.Service_Get_REC                                                                     
   join c in _db.encodingMSG                                                                     
   on m.G_Note_ID equals c.EncToId                                                                    
   where c.EncId == 4001 && m.G_Id == 
  service_Get_Data.FirstOrDefault().svr_D.id                                                                     
  orderby m.G_Add_Date                                                                     
  select new                                                                     
  {                                                                         
      id = m.id,                                                                         
      G_Id = m.G_Id,                                                                         
      G_user_name = m.G_user_name,                                                                         
      EncMsgForUser = c.EncMsgForUser,                                                                         
      EncMsgForSvr = c.EncMsgForSvr,                                                                         
      G_Add_Date = m.G_Add_Date,                                                                         
     H_date = m.G_Add_Date.ToString()                                                                    
  }).AsEnumerable().Select(x => new svr_Service_Get_REC                                                                    
  {                                                                        
     id = x.id,                                                                        
     G_Id = x.G_Id,                                                                        
     G_user_name = x.G_user_name,                                                                         
     G_Add_Date = x.G_Add_Date,                                                                         
     EncMsgForUser = x.EncMsgForUser,                                                                     
     EncMsgForSvr = x.EncMsgForSvr,                                                                        
     H_date = x.G_Add_Date.ToString()                                                                     
  }).ToList());
    return View(service_Get_Data.FirstOrDefault());

I need to conver H_date to hijri date in code

H_date = m.G_Add_Date.ToString() 

Same this

ViewBag.H_date = service_Get_Data.FirstOrDefault().svr_M.DateOfBirth.ToString("yyyy/MM/dd", arSA); 

How can I do this directly IN LINQ sql?

Thank you very much for helping me.



Solution

Thank you so much
I have solved the problem
This code is for anyone who wants to use it

    public static Service_Get_Data Get_Data_D(string svrIDE)
{
    Exam_Entity _db = new Exam_Entity();
    CultureInfo arSA = new CultureInfo("ar-SA");
    arSA.DateTimeFormat.Calendar = new HijriCalendar();

    return  ((from m in _db.Svr_M
                             join d in _db.Svr_D
                             on m.svrIDE equals d.svrIDE
                             join b in _db.sch_trans on
                             d.TRN_recIdNew equals b.rec_id
                             join s in _db.Saf_List on
                             b.saf_id equals s.saf_id
                             join u in _db.Sub_List on
                             b.sub_id equals u.sub_id
                             join E in _db.encodingMSG on
                             d.svrNoteID equals E.EncToId
                             join O in _db.Office on
                             b.org_id equals O.OfficeID
                             where d.svrIDE == svrIDE && s.saf_id == u.saf_id && E.EncId == 4001
                             select new
                             {
                                 sch_trans = b,
                                 svr_M = m,
                                 svr_D = d,
                                 saf = s,
                                 sub = u,
                                 encodingMSG = E,
                                 Office = O,
                                 H_DateOfBirth = m.DateOfBirth.ToString(),
                             }
                           ).AsEnumerable().Select(x => new Service_Get_Data
                           {
                               sch_trans = x.sch_trans,
                               svr_M = x.svr_M,
                               svr_D = x.svr_D,
                               saf = x.saf,
                               sub = x.sub,
                               encodingMSG = x.encodingMSG,
                               Office = x.Office,
                               H_DateOfBirth = Convert.ToDateTime(x.H_DateOfBirth).ToString("yyyy-MM-dd", arSA)
                           }).FirstOrDefault());
}


This Question was asked in StackOverflow by Abdullah Al-Mohammadi and Answered by Abdullah Al-Mohammadi It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?