user2785177
user2785177

Reputation: 69

linq date format won't go into list

Trying to set Shortdate list into a Datagrid.ItemsSource

var query = from loan in Loans  
            select new {Date = loan.StatusCommittedDate}  
DataGrid.ItemsSource = query.ToList(); `   

it displays Date in "3/25/2011 12:00:00 AM" Format in the DataGrid
I only need the date, not the time so my Current Linq is

var query = from loan in Loans  
            select new {Date = loan.StatusCommittedDate.ToString()}  
DataGrid.ItemsSource = query.ToList()`  

which displays in "2011-03-25" format which is what I want. So the Linq query works, but inputting it into the datagrid is wrong and if I can get some guidances please. When I try to display the information through a Datagrid in WPF I get this error

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

Upvotes: 1

Views: 239

Answers (1)

Servy
Servy

Reputation: 203820

I'd say the error message is fairly self explanatory. The query provider doesn't know how to translate ToString, so you need to ensure that the query you ask it to translate doesn't include it. You can use AsEnumerable as a way of indicating that the all subsequent operations should be performed on the application side, not translated into the query:

var query = Loans.Select(loan => loan.StatusCommittedDate)
    .AsEnumerable()
    .Select(date => new{ Date = date.ToShortDateString() });

Upvotes: 1

Related Questions