代码之家  ›  专栏  ›  技术社区  ›  ariel

从Linq到SQL查询的自定义日期格式

  •  0
  • ariel  · 技术社区  · 15 年前

    我使用下面的函数返回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
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Guffa    15 年前

    我想是这样的:

    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
    
    推荐文章