Linq 2 Sql DateTime format to string yyyy-MM-dd

耗尽温柔 提交于 2020-01-13 08:10:42

问题


Basically, i need the equivalent of T-SQL CONVERT(NVARCHAR(10), datevalue, 126)

I've tried:

  1. from t in ctx.table select t.Date.ToString("yyyy-MM-dd") but it throws not supported exception
  2. from t in ctx.table select "" + t.Date.Year + "-" + t.Date.Month + "-" + t.Date.Day but i don't think it's an usable solution, because i might need to be able to change the format.

The only option I see is to use Convert.ToString(t.Date, FormatProvider), but i need a format provider, and I'm not sure it works either

FormatProvider doesn't work, String.Format doesn't work (string.Format("{0:yyyy-MM-dd}", t.Date) throws not supported exception too).


回答1:


In case someone else has this problem, I solved this problem by creating a seperate function to do the date formatting for me.

My Linq looks something like this:

from a in context.table
where ...
select new Class
{
DateString = GetFormattedString(a.DateTimeObject)
}

And GetFormattedString just returns DateTimeObject.ToString("yyyy-MM-dd"); This works for me!




回答2:


Assuming that t.Date is nullable (DateTime?) this could be the problem, try using:

from t in ctx.table select (t.HasValue ? t.Date.Value.ToString("yyyy-MM-dd") : string.Empty );

Edit: OK, second try ...

The problem is the translation to SQL it tries to translate the .ToString() to an SQL representation, and fails. So if you should do the following it should work:

(from t in ctx.table select t.Date).ToList().Select(d => d.ToString("yyyy-MM-dd"))

or

(from t in ctx.table select t.Date).AsEnumerable().Select(d => d.ToString("yyyy-MM-dd"))

AsEnumerable() transforms the previously uses IQueryable into an IEnumerable, thus stopping the generation of the SQL (in case of Linq to SQL) or any other transfromation by the provider implementing the specific IQueryable (e.g. Linq to SQL Provider).

Note, before calling AsEnumerable() you should have completed any actions that you want to be converted to SQL and executed on the database directly.




回答3:


Is there a reason to perform the conversion on the database side? Whenever I run into this type of situation, I tend to just allow the database to give me the raw data and then do the massaging and manipulation within the application. Depending on the volume of requests to the database server and the size of the result set, I don't want to tie up processing and response time doing data conversions that can be handled by the client.




回答4:


Try to create an object class that you can set the properties and let the properties be the value for your view.. Set the datetime data from LINQ as string and not datetime.. ex.

//Property class
[DataContract()]
public class Info
{
[DataMember(Name = "created_date")]
public string CreateDate;
}


//Controller
var date = from p in dbContext.Person select p;
CreateDate = Convert.ToDateTime(p.create_date).ToString("yyyy-MM-dd");

Hope you'll try this.. I have this on my past applications and this is what I did.




回答5:


look no.3

var user = (from u in users
                     select new
                     {
                         name = u.name,
                         birthday = u.birthday.Value
                     })
                     .ToList()
                     .Select(x => new User()
                     {
                         name = x.name,
                         birthday = x.birthday.ToString("yyyyMMdd") // 0埋めされるよ
                     });



回答6:


try this

var select = from s in db.Table
             where s.date == someDate
             select new 
             {
                date = DateTime.Parse(s.date.ToString()).ToString("yyyy-MM-dd"),
             };


来源:https://stackoverflow.com/questions/2428128/linq-2-sql-datetime-format-to-string-yyyy-mm-dd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!