views:

48

answers:

1

Im using the folowing function to return a SelectList. I need a custom format on Descr, but replacing Key.ToString() to Key.ToString("DD/MM/YY") render me the error "Method 'System.String ToString(System.String)' has no supported translation to SQL.". How could i use a custom date format on 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
+2  A: 

If you put the conversion in the LINQ to SQL query, it will try to create an SQL query from it. Get the result first, realise it to a list of dates, and make the conversion using LINQ to Objects.

I think it goes like this:

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
Guffa