LINQ to Entities无法识别方法'System.String ToString(System.String)'方法。当我尝试格式化我的日期时

时间:2016-11-03 12:11:37

标签: c# asp.net entity-framework linq entity-framework-6

当我尝试格式化日期并在EF中连接它时,我收到以下错误。

  

LINQ to Entities无法识别方法'System.String   ToString(System.String)'方法,此方法无法翻译   进入商店表达。

var res = ctx.EventLs.Where(a => a.transtime == transDate)
    .Select(a => a.transtime.Value.ToString("yyyyMMdd") + "_" + a.host + "_" + a.seq)
    .ToList();

//var res = ctx.EventLs.Where(a => a.transtime == transDate).Select(a => new
//{
//    text = a.transtime.Value.ToString("yyyyMMdd") + "_" + a.host + "_" + a.seq
//}).ToList();

修改

根据评论:我做以下事情:

   var res = ctx.EventLs.Where(a => a.transtime == transDate).AsEnumerable()
                    .Select(a => a.transtime.Value.ToString("yyyyMMdd") + "_" + a.host + "_" + a.seq).ToList();

它有效。我可以在添加AsEnumerable()之后问为什么会有效吗?

0 个答案:

没有答案