我使用下面的函数返回一个SelectList。我需要在Descr上使用自定义格式,但将Key.ToString()替换为Key.ToString(“DD / MM / YY”)会导致错误“Method'System.String ToString(System.String)'没有支持的翻译到SQL。“我如何在Descr上使用自定义日期格式?
Public Function ReturnDates(ByVal Param_Pesq_Cod As Integer) As SelectList
Dim Qry = From E In DB.Execs _
Where E.Pesq_Cod = Param_Pesq_Cod _
Group E By Key = E.DataHora.Date _
Into Group Order By Key Select New With {.Descr = Key.ToString(), .Val = Key}
Return New SelectList(Qry, "Val", "Descr")
End Function
答案 0 :(得分:2)
如果将转换放在LINQ to SQL查询中,它将尝试从中创建SQL查询。首先获得结果,将其实现为日期列表,然后使用LINQ to Objects进行转换。
我认为它是这样的:
Public Function ReturnDates(ByVal Param_Pesq_Cod As Integer) As SelectList
Dim Qry = From E In DB.Execs _
Where E.Pesq_Cod = Param_Pesq_Cod _
Group E By Key = E.DataHora.Date _
Into Group Order By Key Select Key
Dim List = _
Qry.ToList() _
.Select(Function(d) New With {.Descr = d.ToString(), .Val = d})
Return New SelectList(List, "Val", "Descr")
End Function