Linq-to-Entities:选择查询表达式中的格式日期

时间:2009-11-20 19:33:30

标签: datetime linq-to-entities string-formatting

我试图直接从LINQ-to-Entities查询表达式获取格式化的日期字符串。

nonBusinessDays = (from ac in db.AdminCalendar
                   where ac.DateTimeValue >= calendarStartDate && ac.DateTimeValue <= calendarEndDate && ac.IsBusinessDay == false
                   select ac.MonthValue + "/" + ac.DayOfMonth + "/" + ac.FullYear).ToList();

但是,我收到了folloinw错误消息: “无法将类型'System.Nullable`1'强制转换为'System.Object'.LINQ to Entities仅支持转换实体数据模型基元类型。”

除了遍历结果集之外,有没有办法做到这一点? 谢谢! 安倍

2 个答案:

答案 0 :(得分:9)

我找到了一个解决方法:

 nonBusinessDays = (from dt in
                            (from ac in db.AdminCalendar
                             where ac.DateTimeValue >= calendarStartDate && ac.DateTimeValue <= calendarEndDate && ac.IsBusinessDay == false
                             select ac.DateTimeValue).ToList()
                    select string.Format("{0:M/d/yyyy}", dt)).ToList();

答案 1 :(得分:0)

尝试更改您的选择以使用ToString:

nonBusinessDays = (from ac in db.AdminCalendar
                   where ac.DateTimeValue >= calendarStartDate && ac.DateTimeValue <= calendarEndDate && ac.IsBusinessDay == false
                   select ac.DateTimeValue.ToString("MM/dd/yyyy")).ToList();