在 linq 表达式中使用 ToString() 转换 DateTime 值
using ToString() in linq expression to convert DateTime value
我正在尝试将 DateTime 值转换为字符串,但出现此运行时错误:
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be
translated into a store expression.
这是我正在使用的查询:
using (var db = new DbContext())
{
var results = (from u in db.AspNetUserses
join upi in db.UserPersonalInfoes on u.Id equals upi.UserId into upis
from upi in upis.DefaultIfEmpty()
join up in db.UserPreferenceses on u.Id equals up.UserId into ups
from up in ups.DefaultIfEmpty()
join us in db.UserStatses on u.Id equals us.UserId into uss
from us in uss.DefaultIfEmpty()
select new
{
Username = u.UserName,
Telephone = (upi == null ? String.Empty : upi.Telephone),
ID = u.Id,
LastLogin = (us == null ? String.Empty : us.LastLoginDate)
}).ToList();
gvUsers.DataSource = results;
gvUsers.DataBind();
}
知道如何解决这个错误吗?
创建第二个列表并在那里进行转换。
using (var db = new DbContext())
{
var results = (from u in db.AspNetUserses
join upi in db.UserPersonalInfoes on u.Id equals upi.UserId into upis
from upi in upis.DefaultIfEmpty()
join up in db.UserPreferenceses on u.Id equals up.UserId into ups
from up in ups.DefaultIfEmpty()
join us in db.UserStatses on u.Id equals us.UserId into uss
from us in uss.DefaultIfEmpty()
select new
{
Username = u.UserName,
Telephone = (upi == null ? String.Empty : upi.Telephone),
ID = u.Id,
LastLogin = (us == null ? DateTime.MinValue : us.LastLoginDate)
}).ToList();
var list = (from r in results
select new {
Username = r.UserName,
Telephone = r.Telephone,
ID = r.ID
LastLogin = r.LastLogin == DateTime.MinValue ? "" : r.LastLogin.ToString()
}).ToList();
gvUsers.DataSource = list;
gvUsers.DataBind();
}
我正在尝试将 DateTime 值转换为字符串,但出现此运行时错误:
Additional information: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
这是我正在使用的查询:
using (var db = new DbContext())
{
var results = (from u in db.AspNetUserses
join upi in db.UserPersonalInfoes on u.Id equals upi.UserId into upis
from upi in upis.DefaultIfEmpty()
join up in db.UserPreferenceses on u.Id equals up.UserId into ups
from up in ups.DefaultIfEmpty()
join us in db.UserStatses on u.Id equals us.UserId into uss
from us in uss.DefaultIfEmpty()
select new
{
Username = u.UserName,
Telephone = (upi == null ? String.Empty : upi.Telephone),
ID = u.Id,
LastLogin = (us == null ? String.Empty : us.LastLoginDate)
}).ToList();
gvUsers.DataSource = results;
gvUsers.DataBind();
}
知道如何解决这个错误吗?
创建第二个列表并在那里进行转换。
using (var db = new DbContext())
{
var results = (from u in db.AspNetUserses
join upi in db.UserPersonalInfoes on u.Id equals upi.UserId into upis
from upi in upis.DefaultIfEmpty()
join up in db.UserPreferenceses on u.Id equals up.UserId into ups
from up in ups.DefaultIfEmpty()
join us in db.UserStatses on u.Id equals us.UserId into uss
from us in uss.DefaultIfEmpty()
select new
{
Username = u.UserName,
Telephone = (upi == null ? String.Empty : upi.Telephone),
ID = u.Id,
LastLogin = (us == null ? DateTime.MinValue : us.LastLoginDate)
}).ToList();
var list = (from r in results
select new {
Username = r.UserName,
Telephone = r.Telephone,
ID = r.ID
LastLogin = r.LastLogin == DateTime.MinValue ? "" : r.LastLogin.ToString()
}).ToList();
gvUsers.DataSource = list;
gvUsers.DataBind();
}