LINQ查询将字符串转换为日期时间 [英] LINQ Query to Convert string to datetime

查看:1158
本文介绍了LINQ查询将字符串转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串值转换为日期和时间

I want to convert the string value to date time

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;} 
    }
}

查询

var DateQuery = (from o in db.Order_Reports
                 select new demoDate {
                    DueDate = System.DateTime.Parse(o.ReportDueDateTime), 
                    OrderReportID = o.OrderReportID
                 }).ToList();

此代码显示了以下错误

LINQ到实体不能识别方法的System.DateTime解析(System.String)的方法,而这种方法不能被翻译成店前pression。

LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method, and this method cannot be translated into a store expression.

推荐答案

您需要先兑现查询(例如加载数据)的应用程序,然后解析它。实体框架不知道如何执行.NET方法(如 DateTime.Parse ),它只知道如何把它们翻译成 SQL

You need to first materialize the query (i.e. load the data) to your application and then parse it. Entity Framework doesn't know how to execute .Net methods (like DateTime.Parse), it only knows how to translate them to SQL

var DateQuery = db.Order_Reports.ToList().Select(o => new demoDate 
{
    DueDate=DateTime.Parse(o.ReportDueDateTime),
    OrderReportID= o.OrderReportID
});

这篇关于LINQ查询将字符串转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆