且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从可为空的日期时间列LINQ中删除时间

更新时间:2023-10-14 21:34:16

如果我正确理解您的意思,则只想返回日期部分(忽略时间部分).为 F-ES Sitecore [EntityFunctions.TruncateTime方法(System.Data.Objects) [ ^ ]截断时间,但不会将其删除.要返回日期字符串列表,您必须使用类似于以下内容的内容:

If i understand you correctly, you want to return only date part (ignore time part). As F-ES Sitecore[^] mentioned in the comment to the question, EntityFunctions.TruncateTime Method (System.Data.Objects)[^] truncates the time but doesn''t remove it. To return a list of date-strings, you have to use something similar to:

var result = string.Join(",", db_context
	.Where(x => x!=null) //get non null values
	.ToList() //<=this might be needed within EF
	.Select(x => x.Value.ToString("dd/MM/yyyy", provider))
	.Distinct());  //get non-duplicates 


结果:


Result:

21/03/2017,01/05/2017,08/05/2017,21/05/2017