加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq的短日期

发布时间:2020-12-16 02:01:18 所属栏目:百科 来源:网络整理
导读:我希望我的查询停止显示时间和日期.这是我试过的: 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
我希望我的查询停止显示时间和日期.这是我试过的:

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= (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();

两者都没用.

LINQ to Entities无法识别方法’System.String ToString(System.String)’方法,并且此方法无法转换为商店表达式.在尝试应用字符串方法时.

解决方法

您需要在绑定时执行格式化.由于您没有显示实际的绑定代码,因此很难专门解决您的情况,但让我们看看您的查询中会发生什么:

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();

LINQ处理此查询后,生成的Query变量应为List< DateTime>类型.您使查询工作的方式将返回一个DateTimes列表,格式如下:

2014-04-23 00:00:00
2014-03-28 00:00:00
etc...

为了在没有时间值的情况下绑定它,您需要在绑定时对列表的每个元素(或所需元素)调用ToString().

假设您正在使用ListBox或类似的东西,您可以编写以下内容:

foreach (var date in myList) //this is the resultant list from the query
{
    listBox1.Items.Add(date.ToString("MM/dd/yyyy");
}

如果您确实绑定到DataSource属性,则需要转换List< DateTime>到列表< string>使用格式化的值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读