如何使用linq将字符串转换为日期时间到实体....
我有以下查询,其中visit_date列数据类型是字符串...
var memberl = from v in abc.visits
join m in abc.members on v.member_Id equals m.member_Id
where Convert.ToDateTime(v.visit_Date) >= startdate &&
Convert.ToDateTime(v.visit_Date) <= enddate
group m by new { m.member_Firstname,
m.member_Lastname, m.member_Id } into g
orderby g.Count()
select new
{
numVisits = g.Count(),
firstname = g.Key.member_Firstname,
lastname = g.Key.member_Lastname
};
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法改变架构......
我有错误:
linq to entites does not recognise Convert.ToDatetime method
Run Code Online (Sandbox Code Playgroud)
是否有任何可能的解决方案将字符串转换为Datetime?
更新的代码:
根据要求我更新了我的问题
var data = (from v in abc.visits
join m in abc.members on v.member_Id equals …Run Code Online (Sandbox Code Playgroud) 我想将字符串值转换为日期时间
类
public class demoDate
{
public DateTime DueDate;
public int OrderReportID;
public DateTime _DueDate
{
get { return DueDate; }
set { DueDate = value; }
}
public int _OrderReportID
{
get { return OrderReportID; }
set { OrderReportID = value;}
}
}
Run Code Online (Sandbox Code Playgroud)
询问
var DateQuery = (from o in db.Order_Reports
select new demoDate {
DueDate = System.DateTime.Parse(o.ReportDueDateTime),
OrderReportID = o.OrderReportID
}).ToList();
Run Code Online (Sandbox Code Playgroud)
此编码显示以下错误
LINQ to Entities无法识别方法'System.DateTime Parse(System.String)'方法,并且此方法无法转换为存储表达式.
我必须使用"convert to datetime"方法来比较日期时间与参考月份和年份,如下所示
var res = db.Table1
//other tables, and where conditions
.Select(x => new MyObj
{
//...
Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddMonths(Convert.ToDateTime("01/" + x.month + "/" + x.year), 1))).Description
})
Run Code Online (Sandbox Code Playgroud)
我知道Convert.ToDateTime无法从提供者的sql中翻译,我需要一个想法来做那个查询.
更新:我不会在转换之前实现查询,因为这显然意味着获取所有记录.