LINQ Query to Convert string to datetime

.net c# datetime entity-framework linq

Question

The string value has to be converted to a date and time.

Class

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

Query

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

These codes display the following error

The method "System.DateTime Parse(System.String)" is not supported by LINQ to Entities, and it cannot be converted into a store expression.

1
9
1/18/2014 11:33:58 PM

Accepted Answer

Prior to parsing the query, you must materialize it, or load the data into your application. Entity Framework lacks execution skills. Net techniques, likeDateTime.Parse ), it only understands how to convert them toSQL

var DateQuery = db.Order_Reports.ToList().Select(o => new demoDate 
{
    DueDate=DateTime.Parse(o.ReportDueDateTime),
    OrderReportID= o.OrderReportID
});
7
6/3/2014 10:41:19 AM

Popular Answer

SqlFunctions can be used to transform it if you need to use SQL. Simply add a zero-interval DateAdd.

var DateQuery = db.Order_Reports.Select(o => new demoDate {
    DueDate = SqlFunctions.DateAdd("day", 0, o.ReportDueDateTime), 
    OrderReportID = o.OrderReportID
 });


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow