Error in expression

0

Can anyone help me with the following error?

Here is my code below. I'm 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;
}
    
asked by anonymous 22.03.2018 / 15:15

1 answer

1

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

To solve, do the expression processing before and just pass 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 it to test itself since I do not know if it will respect the order of execution in the expression or not (I do not currently have the compiler to test).

    
22.03.2018 / 15:49