Entity Framework 4 / Linq: How to convert from DateTime to string in a query?

后端 未结 4 1125
猫巷女王i
猫巷女王i 2020-12-05 18:22

I have the following query:

from a in Products
select new ProductVM
    {
         id = a.id,
         modified = a.modified.ToString()
    }
4条回答
  •  一整个雨季
    2020-12-05 18:55

    Here's an alternative:

    .Select( p -> SqlFunctions.StringConvert((double)
                      SqlFunctions.DatePart("m", p.modified)).Trim() + "/" +
                  // SqlFunctions.DateName("mm", p.modified) + "/" + MS ERROR?
                  SqlFunctions.DateName("dd", p.modified) + "/" +
                  SqlFunctions.DateName("yyyy", p.modified)
    

    Apparently DateName("MM", ..) spells out the month name where DatePart("mm", ..) provides a numeric value, thus the StringConvert( ), but this left pads the result with spaces, thus the .Trim().

提交回复
热议问题