Convert datetime to a formatted string inside a LINQ-to-entities query

前端 未结 6 908
时光取名叫无心
时光取名叫无心 2020-12-13 19:15

How can I convert DateTime into a formatted string?

This is the line in the following query that needs help:

StartDate = string.Format(\         


        
6条回答
  •  清歌不尽
    2020-12-13 19:25

    In vb (valid to c# too changing syntax):

    Imports System.Data.Entity
    ... 
    query.Select(Function(x) New MyObject With {
        ...
        .DateString = DbFunctions.Right("00" & x.DateField.Day, 2) & "/" & DbFunctions.Right("00" & x.DateField.Month, 2) & "/" & x.DateField.Year
        ...
    }).ToList()
    

    Note: ToList(), ToEnumerable() are not the way because its executes a query, the user wants linq to sql..

提交回复
热议问题