我有这一行属性
Public ReadOnly Property DateToUniversal(ByVal dt As Nullable(Of Date)) As String
Get
Return If(dt.HasValue, dt.Value.ToString("u").Substring(0, 10), "")
End Get
End Property
如果我正在尝试将其用于
from n in mydatacontext
select new myObj {n.field1,n.field2, DateToUniversal(n.field3)}
它不能一直工作,因为LINQ2SQL无法将其转换为sql查询。
有办法做到这一点吗?
答案 0 :(得分:1)
您必须将其转换为两个查询:
var query = (from n in mydatacontext select new {n.field1,n.field2, n.field3}).AsEnumerable();
query = from n in query select new {n.field1, n.field2, DateToUniversal(n.field3)};
这一点非常重要。请致电query
IEnumerable
而不是var
(或IQueryable
)AsEnumerable()
,这一点很重要你得到一个IEnumerable<T>
,因为我们想强制第二个查询在第一个(LINQ to SQL)查询的结果上使用LINQ to Objects。
此时我不在VS面前,但这应该可以解决问题。
感谢itowlson指出它必须是IEnumerable<T>
,而不仅仅是IEnumerable
!