asp.net-mvc LINQ 将日期时间转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6887776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:22:43  来源:igfitidea点击:

LINQ convert DateTime to string

asp.net-mvclinqentity-frameworkentity

提问by Ronald

List<Post> list =
(
    from c in db.TitleComments
    join t in db.Titles on c.TitleId equals t.Id
    join u in db.Users on c.UserId equals u.Id
    where t.Id == _titleId && c.Date > time
    orderby c.Date descending
    select new Post { Username = u.Username, PostingDate = c.Date.ToString(), Data = c.Comment }
).ToList();

The code above causes exception on the convertion of date to string, PostingDate = c.Date.ToString(). Any ideas how to get around this?

上面的代码在将日期转换为字符串时会导致异常,PostingDate = c.Date.ToString()。任何想法如何解决这个问题?

Exception error: {"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."}

异常错误:{“LINQ to Entities 无法识别方法 'System.String ToString()' 方法,并且此方法无法转换为存储表达式。”}

回答by BrunoLM

linq is trying to convert date to string using sql but since there is no ToString() method in sql it can't convert it, this behavior is by design- Joakim

linq 正在尝试使用 sql 将日期转换为字符串,但由于 sql 中没有 ToString() 方法,因此无法将其转换,此行为是设计使然 - Joakim

In other words, return the date itself and convert it to a string after it executes on SQL side:

换句话说,返回日期本身并在它在 SQL 端执行后将其转换为字符串:

(
select new { Username = u.Username,
    PostingDate = c.Date
    [...]
})
.ToList() // runs on SQL and returns to the application
.Select(o =>  // is not generating a SQL, it is running on the app
    new Post { Username = o.Username,
        PostingDate = o.PostingDate.ToString(),
        [...]
    })

回答by Anthony Pegram

You can remedy your problem by projecting into an anonymous type, and then at a later step project into Postafter the data has already been returned from the DB.

您可以通过投影到匿名类型来解决您的问题,然后在Post数据已经从数据库返回后的稍后步骤投影到。

(from ....
 select new { /* stuff */, Date = c.Date })
.AsEnumerable()
.Select(p => new Post { /* stuff */, PostingDate = p.Date.ToString() })
.ToList();

However, given that you have a property called PostingDate, the original source being a date, I would recommend your revise your object to actually keep the value as a DateTimeinstead of a string.

但是,鉴于您有一个名为PostingDate的属性,原始来源是 date,我建议您修改对象以实际将值保留为 aDateTime而不是字符串。

回答by Baz1nga

I dont think this can be done in a direct way.

我认为这不能以直接的方式完成。

   var list =    
    select new Post { Username = u.Username, PostingDate =  SqlFunctions.StringConvert(c.Date), Data = c.Comment } 
from 
(from c in db.TitleComments
    join t in db.Titles on c.TitleId equals t.Id
    join u in db.Users on c.UserId equals u.Id
    where t.Id == _titleId && c.Date > time
    orderby c.Date descending).AsEnumerable()  
    ).ToList();

Also with EF4 you can try something like this:

同样使用 EF4,您可以尝试这样的操作:

List<Post> list =
(
from c in db.TitleComments
join t in db.Titles on c.TitleId equals t.Id
join u in db.Users on c.UserId equals u.Id
where t.Id == _titleId && c.Date > time
orderby c.Date descending
select new Post { Username = u.Username, PostingDate = SqlFunctions.DateName(c.Date), Data = c.Comment }
).ToList();