Ho una classe Entity con data/ora archiviato, desidero selezionare il valore distinto 'mon-yyyy' nel formato archiviato datetime e popolare l'elenco a discesa.La data di formattazione nella query Linq-to-Entities causa l'eccezione
il seguente codice mi dà l'errore: messaggio
var env = db.Envelopes.Select(d => new
{
d.ReportDate.Year,
d.ReportDate.Month,
FormattedDate = d.ReportDate.ToString("yyyy-MMM")
}).Select(d => d.FormattedDate)
List<SelectListItem> _months = new List<SelectListItem>();
foreach (var mname in env)
{
_months.Add(new SelectListItem() { Text = mname, Value = mname });
}
Errore:
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
Come posso correggere questo messaggio di errore?
Grazie SR
@Anthony Pegram: grazie per la risposta. dal tuo codice voglio ottenere il valore distinto e ordinare da ReportDate in ordine decrescente. dove ho bisogno di aggiungere questi due nel tuo codice. – sfgroups
Ho aggiornato la risposta per includere un ordinamento per data discendente e un distinto prima della chiamata 'AsEnumerable()'. Questi saranno quindi parte di SQL e gestiti dal database. –
@Anthony Pegram, scusa se la mia domanda non è chiara, penso. Voglio ottenere il valore distinto 'yyyy-MMM', non il distinto reportdate. – sfgroups