1

LINQ to Entity Framework 4.0. SQL Server.

I'm trying to return a list of objects and one of the columns in the database is varchar(255) and contains dates. I'm trying to cast the value to datetime, but I haven't found the solution to that yet.

Example:

    List<MyObject> objects = (from c in context.my_table
                              where c.field_id == 10
                              select new MyObject()
                              {
                                  MyDate = c.value // This is varchar, want it to be datetime
                              }).ToList();

Is this not possible?

Update. This is LINQ to Entity. When trying to convert to DateTime I get:

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.
GregInWI2
  • 904
  • 1
  • 17
  • 26

4 Answers4

1

The answer is it's not currently possible with Entity Framework. With LINQ itself it is, just not supported with Entity Framework.

GregInWI2
  • 904
  • 1
  • 17
  • 26
0

You want DateTime.Parse(c.value) which will take a string containing a date and create a DateTime object out of it.

Marcie
  • 1,169
  • 1
  • 10
  • 17
  • 2
    I get this 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. – GregInWI2 Dec 03 '10 at 19:38
0

you can do like this Date=DateTime.Parse(text)

Marco Leo
  • 516
  • 4
  • 9
0

read, And the best bet would be using your result and then Converting to date time. Like below,

 static void Main(string[] args)
     {
        Console.WriteLine(getme());
        Console.ReadLine(); 
    }
    private static DateTime  getme()
    {
        List<string> ss = new List<string>();
        ss.Add("11/11/2010");
        var r = from l in ss
                select new { date = Convert.ToDateTime(l) };



        return r.FirstOrDefault().date;

    }
indiPy
  • 7,844
  • 3
  • 28
  • 39
  • This the Entity Framework. I'm return data from SQL Server. This version 4.0. Convert doesn't work, gives me an error. I updated my question with the error message. – GregInWI2 Dec 03 '10 at 19:45
  • The dates are all good, Entity Framework never supported Convert.To. – GregInWI2 Dec 03 '10 at 20:05