我有以下LINQ to实体查询(使用odp.net):
Dim query = from Elemento in X.VIEW where Elemento.code = "10" Select New With {Elemento.code, .Time = Elemento.Time.ToString("HH:mm")} query.ToList()
代码在ToList()
方法上抛出异常:
LINQ to Entities does not recognize the method 'System.String ToString()'
我已经阅读了所有关于此的内容,但还没有找到一个简单的解决方法。
Lazyberezovsky有正确的答案,我以前无法让它工作,因为我这样做:
Dim query = (from Elemento in X.VIEW where Elemento.code = "10" Select New With {Elemento.code, Elemento.Time}).ToList Dim query2 = from elemento in query Select New With {elemento.code, TIME = elemento.Time.ToString("HH:mm")} Dim result = query2.ToList()
但这不起作用,显然你必须一步完成。
答案 0 :(得分:6)
您可以将DateTime转换为字符串内存。只需在转换时间之前拨打ToList
电话:
在C#中:
var query = from Elemento in X.VIEW
where Elemento.code == "10"
select new { Elemento.code, Elemento.Time };
var result = query.ToList() // now you are in-memory
.Select(x => new { x.code, Time = x.Time.ToString("HH:mm") });
在VB.Net中:
Dim query = From Elemento In X.VIEW
Where Elemento.code = "10"
Select New With {Elemento.code, Elemento.Time}
Dim result = query.ToList() _
.Select(Function(x) New With {x.code, .Time = x.Time.ToString("HH:mm")})
BTW为什么要在结果中选择Elemento.code
,如果你在哪里运算符(它总是等于“10”)进行过滤。