C# 林克的短日期
我希望我的查询停止显示时间,只显示日期。这就是我一直试图做到的:C# 林克的短日期,c#,linq,C#,Linq,我希望我的查询停止显示时间,只显示日期。这就是我一直试图做到的: Query= (from z in ctx.Interactions where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false select new {
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = new DateTime(z.ActivityDate.Year, z.ActivityDate.Month, z.ActivityDate.Day),
Subject = z.Subject
}).ToList();
Query=(从ctx.Interactions中的z开始)
其中z.ActivityDate=EndDateTo
&&z.深度==假
选择新的
{
日期=新日期时间(z.ActivityDate.Year,z.ActivityDate.Month,z.ActivityDate.Day),
主语
}).ToList();
及
Query=(从ctx.Interactions中的z开始)
其中z.ActivityDate=EndDateTo
&&z.深度==假
选择新的
{
日期=z.ActivityDate.Date,
主语
}).ToList();
两个都不起作用
LINQ-to-Entities无法识别方法“System.String-ToString(System.String)”方法,此方法无法转换为存储表达式。
尝试应用String方法时。您可以使用anyDate.ToString(“ddMMyyyy”)//任何首选格式。
不确定这是否是你要找的 查询返回具有日期和主题属性的对象 在Date属性中,传递的是DateTime对象。为了显示短日期,在日期上有一个“ToSortDateString()”函数 如果您不想处理日期,而更喜欢选择字符串,那么在linq查询中进行转换 如果要返回字符串,请使用此选项:
var q = (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date.ToShortDateString(), Subject = z.Subject }).ToList();
var q=(从ctx.Interactions中的z开始)
其中z.ActivityDate=EndDateTo&&z.Indepth==false
选择新建{Date=z.ActivityDate.Date.ToSortDateString(),Subject=z.Subject}).ToList();
可能会对您有所帮助
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo
&& z.ActivityDate >= EndDateTo
&& z.Indepth == false
select new
{
Date = z.ActivityDate.ToShortDateString(),
Subject = z.Subject
}).ToList();
Query=(从ctx.Interactions中的z开始)
其中z.ActivityDate=EndDateTo
&&z.深度==假
选择新的
{
Date=z.ActivityDate.ToShortDateString(),
主语
}).ToList();
您需要在绑定时执行格式化。由于您没有显示实际的绑定代码,因此很难具体说明您的情况,但让我们看看查询中发生了什么:
Query= (from z in ctx.Interactions
where z.ActivityDate <= StartDateTo && z.ActivityDate >= EndDateTo && z.Indepth == false
select new { Date = z.ActivityDate.Date, Subject = z.Subject }).ToList();
要在不使用时间值的情况下绑定此,需要在绑定时对列表的每个元素(或所需元素)调用ToString()
假设您使用的是列表框
或类似的东西,您可以编写以下内容:
foreach (var date in myList) //this is the resultant list from the query
{
listBox1.Items.Add(date.ToString("MM/dd/yyyy");
}
如果您确实要绑定到
数据源
属性,则需要将列表
转换为带有格式化值的列表。将日期转换为字符串,如下所示
string stringDate=string.empty;
stringDate=Convert.ToDateTime("2014-04-23 00:00:00").ToShortDateString();
它将提供如下输出:
2014-04-23LINQ查询不会显示任何内容。您想在哪里只显示日期?您需要将日期/时间转换为字符串以显示。Linq不显示任何内容,但当我显示它时,它会显示日期。它绑定到查询“我希望我的查询停止显示时间”-DateTime
不包含任何格式信息,因此如果DateTime
字段只有日期,则其时间部分将设置为00:00
。您需要将DateTime
对象格式化为字符串。请参见DateTime
有一个Date
部分和一个Time
部分,顾名思义。如果您只想查看日期
部分,则必须以这种方式对其进行格式化(例如Date.ToShortDateString()
)。如果使用z.ActivityDate.Date.ToShortDateString()
,仍然会导致错误,因为其中包含日期
z.ActivityDate.ToSortDateString()
应该可以改为。我尝试了您的建议,但出现了异常“System.NotSupportedException:'LINQ to Entities无法识别方法'System.String ToSortDateString()'方法,并且此方法无法转换为存储表达式。”“我想您没有阅读此问题。它在LINQ查询中。
foreach (var date in myList) //this is the resultant list from the query
{
listBox1.Items.Add(date.ToString("MM/dd/yyyy");
}
string stringDate=string.empty;
stringDate=Convert.ToDateTime("2014-04-23 00:00:00").ToShortDateString();