Converting string to datetime in mapping to viewmodel

asp.net-mvc c# datetime entity-framework-6

Question

I have an MVC app where I collect DateTime fields (MM/dd/yyyy) as strings to avoid Chrome overriding the datepicker in bootstrap and to avoid mapping in sqlbulk upload of Excel. I now need to map this string field back to datetime and am struggling with this greatly.

Here's my Model:

public class Something
{
    [Key]
    public string SomeNumber { get; set; }
        ....
    public string SomeDate { get; set; }
    public string SomeOtherDate { get; set; }
        ....
}

and my ViewModel:

public class HistoricalDataVM
{
    .....

    [Display(Name = "Some Date")]
    public DateTime SomeDate { get; set; }

    [Display(Name = "Some Other Date")]
    public DateTime SomeOtherDate { get; set; }

    ....
}

and my controller action:

[ChildActionOnly]
public PartialViewResult SomePartial()
{

    var vm = _ctx.Something.Select(p => new HistoricalDataVM()
    {

        ...

        SomeDate = DateTime.ParseExact(p.SomeDate, "MM/dd/yyyy", CultureInfo.InvariantCulture),
        SomeOtherDate = DateTime.ParseExact(p.SomeOtherDate, "MM/dd/yyyy", CultureInfo.InvariantCulture),

        ....

    }).OrderByDescending(c => c.SomeDate).ToList();

    return PartialView(vm);
}

I have tried "Convert" and DateTime.Parse but all lead to "yellow screen of death" with this error message:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

I have googled and searched SO and can't find a solution that works.

Any help is much appreciated. Thanks.

1
-1
12/27/2016 10:35:39 AM

Accepted Answer

You need to first materialize the query to your application and then parse it. Entity Framework doesn't know how to execute dot Net methods, it only knows how to translate them to SQL

1
6/18/2015 3:41:30 PM

Popular Answer

you can use DateTime.ParseExact() and set your strict format. https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

var datetime = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture);

it should give you something like that :

[ChildActionOnly]
public PartialViewResult SomePartial()
{
    var vm = _ctx.Something.Select(p => new HistoricalDataVM()
    {
            SomeDate = DateTime.ParseExact(p.SomeDate, "MM/dd/yyyy", CultureInfo.InvariantCulture),
            SomeOtherDate = DateTime.ParseExact(p.SomeOtherDate, "MM/dd/yyyy", CultureInfo.InvariantCulture),

            ....

        }).OrderByDescending(c => c.SomeDate).ToList();

        return PartialView(vm);
    }


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