从linq的datetime获取日期并显示gridview [英] get date from datetime in linq and show gridview

查看:229
本文介绍了从linq的datetime获取日期并显示gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从linq中的datetime获取日期,如下所示:

  var items = db.Students.ToList()。选择(u => new {bi = u.Birthday.Date}); 

但网格中的项目显示如下:





我看过其他问题:



问题1: Linq - 从DateTime选择日期



但是当我编辑代码同样地,

  var items =(from xx in db.Students 
select new
{
EntityFunctions.TruncateTime(xx.Birthday)
})ToList();

我在代码中出现错误!!



问题2: 如何仅获取日期从datetime列使用linq



但接受这个问题的答案将日期转换为字符串!



问题3: 临床到实体从DateTime获取日期



答案类似于question1。



aspx for show grid:

 < asp:GridView ID =GridView1runat =server> 
< / asp:GridView>有没有人有解决这个问题的想法?


$ b $(



谢谢。

解决方案

正如你以前在其他问题中所说的一样;当您在某些时候显示您的网格 DateTime.ToString()被调用时,会导致显示日期和时间的默认行为。 DateTime.Date property只返回DateTime的Date部分作为DateTime;换句话说,它返回相同的事情,但时间设置为正好是12:00 AM。



要获取唯一的日期作为字符串,您想要调用 DateTime.ToShortDateString ,请参阅这里: https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring(v = vs.110).aspx



简而言之,这与Linq无关,当您尝试在网格中显示DateTime对象时,会出现问题,因此需要通过调整网格单元格的格式来解决问题。


I need to get date from datetime in linq like this:

var items = db.Students.ToList().Select(u => new { bi = u.Birthday.Date });

but items in grid are displayed as follows:

I've seen other questions:

Question1: Linq - Select Date from DateTime

but when I edit code Similarly,

                var items = (from xx in db.Students                                     
                                 select new
                                 {
                                    EntityFunctions.TruncateTime(xx.Birthday)
                                 }).ToList();

I got a error in code!!

Question2: How to get only Date from datetime column using linq

but accepted answer to this question convert date to string!

Question3: Linq to Entity get a Date from DateTime

Answers similar to question1.

aspx for show grid:

<asp:GridView ID="GridView1" runat="server">            
</asp:GridView>

Does anyone have an idea to solve this problem?

Thanks.

解决方案

As you have been told before in your other questions; when you display your grid at some point DateTime.ToString() is called, this results in a default behavior of displaying date and time. The DateTime.Date property simply returns the date part of the DateTime as a DateTime; in other words it returns the same thing but with the time set to exactly 12:00 AM.

To get the only the date as a string, you want to call DateTime.ToShortDateString, see here: https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring(v=vs.110).aspx

In short this has nothing to do with Linq, the problem occurs when you try to display your DateTime object within your grid and as such needs to be solved by adjusting the formatting of the grid cells.

这篇关于从linq的datetime获取日期并显示gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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