-2

I need to get date from datetime in linq like this:

var items = db.Students.ToList().Select(u => new { bi = u.Birthday.Date });

but items in grid are displayed as follows:

grid

How can I fix it?

Thanks.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55

1 Answers1

4

The Method string.Format can't betranslated into a SQL statement. But you can materialize the rows first .ToList() and string.Format afterwards.

var items = db.Students.ToList().Select(u => new { bi = string.Format("{0:MM/dd/yyyy}", u.Birthday) });
fubo
  • 44,811
  • 17
  • 103
  • 137
  • The power of Linq sometimes works against it ;-) It is so powerful you can't imagine there a things it can't do. But when working on non-.NET sources like SqlServer Linq sometimes just can't 'translate' the Linq to a SQl statement. The message "can't translate in a store expression" isn't really clear for that situation. – Michel Jul 07 '16 at 07:56
  • 2
    @Michel what do you mean? The error is *very* clear - you can't use String.Format and that's it. In fact, trying to format strings in SQL is a bad idea in general, even if a `FORMAT` function is available – Panagiotis Kanavos Jul 07 '16 at 07:57
  • Thanks for response but I need to date format to sort items. – Ali Soltani Jul 07 '16 at 08:01
  • I edited my question. I need to date format. – Ali Soltani Jul 07 '16 at 08:06
  • @PanagiotisKanavos: I don't think that when you first encounter this problem (that Linq can't translate your Linq into Sql) that .NET makes a clear hint of that with the message "can't translate in a store expression" – Michel Jul 07 '16 at 09:23