如何从linq中的datetime数据中仅检索日期

如何从linq中的datetime数据中仅检索日期

本文介绍了如何从linq中的datetime数据中仅检索日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实要求检索包含数据库中datetime类型的日期col的数据,现在我应该以dd-mm-yyyy的形式截断日期和返回日期的时间, linq查询 - 我已经搜索了更多的时间来获得这个,但无法以我上面提到的正确格式检索,我的查询如下。

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.

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!!!

推荐答案

CurrentDate = c.CurrentDate.Date,



如果你想把日期作为一个字符串(任何我建议反对它),那么只需使用ToShortDateString方法: []因为它返回一个适合代码运行的PC文化的字符串。


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.


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





这将要求CurrentDate属于DateTime数据类型。您可以在转换为字符串 []。那不是使用ToString,但在这种情况下String.Format()是相同的。



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.


这篇关于如何从linq中的datetime数据中仅检索日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 23:02