10

I want to convert the string value to date 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();

This coding shows following error

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

6
  • try use Convert.ToDateTime instead Commented Jan 10, 2014 at 8:05
  • 1
    You'll need to materialize db.Order_Reports (e.g. .ToList()) before doing the projection, as EF can't parse the convert into a sql expression. Commented Jan 10, 2014 at 8:06
  • @Grundy That too shows error LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression. Commented Jan 10, 2014 at 8:08
  • @StuartLC - I have removed the .ToList() then it shows error at the time of bind grid- LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression. Commented Jan 10, 2014 at 8:12
  • 1
    Not related to your problem, but (a) why do you public fields and properties to access them and (b) why do your property names start with _ instead of the fields? Commented Jan 10, 2014 at 8:26

3 Answers 3

21

If you need convert it with SQL you can try use SqlFunctions.DateAdd and just add zero interval.

var DateQuery = db.Order_Reports.Select(o => new demoDate {
    DueDate = SqlFunctions.DateAdd("day", 0, o.ReportDueDateTime), 
    OrderReportID = o.OrderReportID
 });
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is really clever. It addresses circumstances where the conversion cannot be performed prior to the query while circumventing the need to load the dataset into memory or iterate over the dataset.
6

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

3 Comments

Won't this load the entire table each time it's called? That might be A LOT of data if the table is big.
@RuneAntonsen very true. But unavoidable if you're using regular .Net methods that can't be translated to queries. It would probably be best to redesign this entirely.
or alternatively you can project your columns, then invoke ToList and then apply another select to create the instance
0
DateTime myDateTime = Convert.ToDateTime(DateTimePicker.Text);

var kayitVarmi = (from myfTable in db.tbl_myfTable 
                              .
                              .
                              .
                              .                      
                              where
                                tbl_donemAyYil.donemAyYilKu == myDateTime 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.