Error in expression

Asked

Viewed 44 times

0

Can anyone help me with the following mistake?

enter image description here

Here below is my code. I am trying to fix this error, but I did not succeed.

public IEnumerable<Dia1> GetPendenciasByUser(int centroId)
{
    var query = Db.Dia1S
        .Join(Db.Cadastros, dia1 => dia1.PatientId, cad => cad.PatientId, (dia1, cad) => new { dia1, cad })
        .Join(Db.Randomizacao, dia1 => dia1.dia1.PatientId, rand => rand.PatientId, (dia1, rand) => new { dia1, rand })
        .Where(s => s.dia1.dia1.dtd1 == null ? (Convert.ToInt32(DateTime.Now - s.rand.RandomizacaoData)) > 1 : (Convert.ToInt32(Convert.ToDateTime(s.dia1.dia1.dtd1) - s.rand.RandomizacaoData)) > 1 )
        .Select(s => s.dia1.dia1)
        .ToList();
    return query;
}
  • 1

    that is prq the EF does not know how to manipulate functions that are not in its mapping or... yours (Convert.Toint32(Datetime.Now - s.rand.Randomizacaodata)) ... it does not know convert the function Convert.Toint32( for a Quey validate.... you can see some libraries that help do this, or you can try casting using (int? )(Datetime.Now - s.rand.Randomizacaodata)

  • I understood the casting part, but you can give me an example?

  • Maybe in Functions until the conversion, the problem and we often do not know what you need. that you did as already said does not work.

1 answer

1

There is no SQL command for your Convert.Toint32. When it arrives at the stage of translating its expression to SQL the framework cannot translate a result, generating this error. To avoid this kind of problem you must avoid conversion methods or methods not recognized by the framework.

To resolve do the processing of the expression before and pass only the result inside your Where:

var dateTimeNowAsInteger = Convert.ToInt32(DateTime.Now);

var query = Db.Dia1S
        .Join(Db.Cadastros, dia1 => dia1.PatientId, cad => cad.PatientId, (dia1, cad) => new { dia1, cad })
        .Join(Db.Randomizacao, dia1 => dia1.dia1.PatientId, rand => rand.PatientId, (dia1, rand) => new { dia1, rand })
        .Where(s => s.dia1.dia1.dtd1 == null ? (dateTimeNowAsInteger - s.rand.RandomizacaoData) > 1 : ... )
        .Select(s => s.dia1.dia1)
        .ToList();
    return query;

Your second expression is quite complex since you convert to Datetime and then to int again, in case you can test something like:

(s.dia1.dia1.dtd1 - ((int)s.rand.RandomizacaoData)) > 1

I would like to test it since I do not know if it will respect the execution order in the expression or not (I do not have the compiler at the moment to test).

Browser other questions tagged

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