Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, all i do have a requirement that to retrieve data which consists of date col which of type datetime in database, now i should truncate time from date and return date in the form of "dd-mm-yyyy" , in linq query- i have searched lot more time to get this, but unable to retrieve in a proper format which i have mentioned above, my query is as follows.
C#
IEnumerable<object> objs = (
                from c in this.SPE.Consultants.DefaultIfEmpty<consultant>()
                join r in this.SPE.Recruiters on c.Recruiter equals r.RecruiterId
                join e in this.SPE.States on c.CurrentState equals (int?)e.StateId
                join f in this.SPE.SkillSets on c.SkillSet equals (int?)f.SkillSetId
                join t in this.SPE.Teams on c.Recruiter1.Team equals t.TeamId

                where !c.IsDeleted && (c.Status == chosenvalue) //&& r.IsDeleted == false
                orderby f.SkillSetName
                // where !c.IsDeleted && r.RecruiterId == recid && r.IsDeleted == false
                select new
                {
                    ConsultantId = c.ConsultantId,
                    CurrentDate = c.CurrentDate,---actual column
                  //  CurrentDate = (c.CurrentDate.Date + "-" + c.CurrentDate.Month + "-" + c.CurrentDate.Year)---tried way of one format[and lot more tried],
                    Name = (c.FirstName + " ") + (c.LastName != null ? c.LastName : " "),
                   // CurrentLocation = e.StateName + (c.RelocationArea != null ? " [ " + c.RelocationArea + " ]" : " "),
                    CurrentLocation = e.StateName,
                    RelocationPreference = c.RelocationPreference,
                    RelocationArea = c.RelocationArea,
                    PhoneNumber1 = c.PhoneNumber1,
                    PhoneNumber2 = c.PhoneNumber2,
                    EMailId1 = c.EMailId1,
                    EMailId2 = c.EMailId2,
                    Recruiter = r.RecruiterName,
                    SkillSet = f.SkillSetName,
                    Team = t.TeamName,
                    Reference = c.Reference,
                    OtherComments = c.OtherComments,
                    WorkStatus = c.WorkStatus,
                    Status = c.Status,
                    FieldStatus = (EntityFunctions.DiffDays((DateTime?)c.CurrentDate, (DateTime?)DateTime.Today) > (int?)30 ? "YesWithMonthCrossed" : "No")
                }).AsEnumerable<object>();
            return objs;


please help me to solve this!!!
Posted
Updated 18-Nov-14 2:12am
v2

DateTime.Date returns the DateTime value with the time portion reset to midnight: 0 hours, 0 minues 0 seconds, so just using that on it's own without the other stuff in your commented-out example should work:
C#
CurrentDate = c.CurrentDate.Date,

If you want the date as a string (any I'd advise against it), then just use the ToShortDateString method: MSDN[^] as that returns a string appropriate to the culture of the PC the code runs on.
 
Share this answer
 
If, CurrentDate is the actual column in your database and you want to get the Date in the dd-mm-yyyy format. You can use this code for that,

C#
// Convert the CurrentDate object to string, using a format
c.CurrentDate.ToString("dd-mm-yyyy");


This would require that the CurrentDate is of DateTime data type. You can learn about more formatting for DateTime while converting to string here[^]. That is not using a ToString, but String.Format() is the same in this case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900