0

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.