2010-07-13 67 views
0

使用folowing函數返回一個SelectList。我需要在Descr上的自定義格式,但將Key.ToString()替換爲Key.ToString(「DD/MM/YY」)會給我提供錯誤「Method」System.String ToString(System.String)' SQL。「我如何在Descr上使用自定義日期格式?從Linq到SQL查詢的自定義日期格式

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 

回答

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