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

Abe*_*Abe 5 datetime linq-to-entities string-formatting

我试图直接从LINQ到实体查询表达式获取格式化的日期字符串.

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

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

除了遍历结果集之外,还有什么方法可以做到这一点吗?谢谢!安倍晋三

Abe*_*Abe 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();
Run Code Online (Sandbox Code Playgroud)