error with EF and Mysql query

Asked

Viewed 49 times

2

I have the following consultation:

 var produto = (from a in context.ItensReceita
                join b in context.Receita on a.t0080_id_receita equals b.t0080_id_receita
                join c in context.Medicamento on a.t0080_id_receita equals c.t0051_id_medicamento     
                join d in context.Pessoa on b.t0031_id_paciente equals d.t0031_id_pesso
                   where b.t0020_id_empresa == idEmpresa
                   && Convert.ToDateTime(b.t0080_dt_venda) >= Convert.ToDateTime("2017/01/01")
                   && Convert.ToDateTime(b.t0080_dt_venda) <= Convert.ToDateTime("2017/01/31")
                   select new { b.t0080_dt_venda, d.t0031_nome, c.t0051_nome, a.t0081_lote, a.t0081_qtde, b.t0080_status });

but make that mistake:

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

is not accepting conversation of dates, how can I solve it ?

inserir a descrição da imagem aqui

1 answer

5

Linq does not support the method ToDateTime

What you can do is create a variable and then use it in your query

var data1 = Convert.ToDateTime("2017/01/01");
var data2 = Convert.ToDateTime("2017/01/31");

and use these two variables in your query

var produto = (from a in context.ItensReceita
                join b in context.Receita on a.t0080_id_receita equals b.t0080_id_receita
                join c in context.Medicamento on a.t0080_id_receita equals c.t0051_id_medicamento     
                join d in context.Pessoa on b.t0031_id_paciente equals d.t0031_id_pesso
                   where b.t0020_id_empresa == idEmpresa
                   && DbFunctions.TruncateTime(b.t0080_dt_venda) >= data1
                   && DbFunctions.TruncateTime(b.t0080_dt_venda) <= data2
                   select new { b.t0080_dt_venda, d.t0031_nome, c.t0051_nome, a.t0081_lote, a.t0081_qtde, b.t0080_status });

One detail, is that you will have to use the DbFunctions.TruncateTime that is part of the namespace System.Data.Entity

  • Pablo, I tried the way q vc suggested and gave the same error, I believe q is by trying to talk about the property && Convert.Todatetime(b.t0080_dt_venda)

  • Yes, I just edited the answer, I had not seen this detail of your dt_venda field

  • One thing, your sale is a Datetime?

  • 1

    no, it’s a string, I’ll have to change right...

  • 1

    It would be nice, @alessandremartins. If you’re saving a date, it’s good to be a camp DateTime, not a string.

  • I think it would be more convenient to change.

  • when trying to use Dbfunctions.Truncatetime, do so in innerException: FUNCTION zulex01.Truncatetime does not exist, I edited the post, see the Debug image

  • Which database you are using?

  • mysql database

  • see if Function of this answer helps you http://stackoverflow.com/a/19731414/2221388 create it in your database

  • well, now did not give the error more than FUNCTION zulex01.Truncatetime does not exist, however Mysql expects the date in format yyyy/MM/dd, Dbfunctions.Truncatetime() asks for a parameter of type datetime, datetime needs to be formatted for yyyyyy/MM/dd, all formatting q axei, the output is a string.

  • Alessandre, I think figuring out how to use Dbfunctions.Truncatetime would fit another question.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.