How to Delete Time From A Datatime In Query LINQ In C #?


I'm consulting a database of Mysql and I want to show the result in DatagridView my problem is that I display the date with the time and I just need the date I tried with




but it returns the following error in execution.


LINQ to Entities does not recognize the 'System.String' method   ToString (System.String) 'of the method, and this method can not be   translate into a warehouse expression.

this is my code.

TEntities db = new TEntities();

var reporte = from consul in db.Transaccion 
              where consul.Cliente_idCliente == idCli 
              select new { Codigo = consul.idTransaccion, 
                           Fecha = consul.fecha.ToString("dd/MM/yyyy"), 
                           Valor = consul.valor, 
                           otra = consul.denominacion + " " + 

dataGridView1.DataSource = reporte.ToList() ; 
asked by byariel 07.12.2018 в 15:24

1 answer


Your problem occurs because Linq to Entities does not have a definition for ToString() .

To be able to do it with ToString() you could execute AsEnumerable() (which will cause the query to the database to be executed) and then, having already executed the query, the use of ToString() would be valid.

For example:

var reporte = db.Transaccion.Where(c => c.Cliente_idCliente == idCli).AsEnumerable()
                .select(c => 
                    Codigo = c.idTransaccion,
                    Fecha = c.fecha.ToString("dd/MM/yyyy"),
                    Valor = c.valor,
                    otra = c.denominacion + " " + c.noAutorizacion

I clarify that if you handle many records I doubt that this is a very performant solution

Edit: I add the option to configure it from the DataGridView as it raises Orlando De La Rosa that would be clearly better practice than the above

To configure the visualization of the field in a DataGridView , simply modify the property Format of DefaultCellStyle of the required column.

For example, assuming that the first column is where the date is displayed, you could format it like this

dataGridView1.Columns[0].DefaultCellStyle.Format = "dd/MM/yyyy";
answered by 07.12.2018 / 15:55