如何从可为空的日期时间列LINQ中删除时间 [英] How to remove time from nullable datetime column LINQ

查看:180
本文介绍了如何从可为空的日期时间列LINQ中删除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在LINQ查询中遇到问题,请帮助我.

使用select和string.join
通过group by join datetime列进行LINQ查询 这样显示的datetime值
使用string.Join(Environment.NewLine,来自子查询的l.DatetimeColumn)
21/03/2017 11:05:04
01/05/2017 12:24:55
08/05/2017 13:34:12
如果我使用
EntityFunctions.TruncateTime
像这样显示
21/03/2017 00:00:00
01/05/2017 00:00:00

我尝试过的事情:

在LINQ查询中遇到问题,请帮助我

Hi,
I am getting problem in LINQ query please help me.

LINQ query with group by join datetime column using select and string.join
datetime value shown like this
using string.Join(Environment.NewLine, l.DatetimeColumn from subquery)
21/03/2017 11:05:04
01/05/2017 12:24:55
08/05/2017 13:34:12
if i use
EntityFunctions.TruncateTime
it shown like this
21/03/2017 00:00:00
01/05/2017 00:00:00

What I have tried:

Getting problem in LINQ query please help me

推荐答案

如果我正确理解您的意思,则只想返回日期部分(忽略时间部分).为 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


这篇关于如何从可为空的日期时间列LINQ中删除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆