0

I Have the following Method:

public List<REP_MEDIDORDISPLAY> GetAllMedidoresDisplay()
{          
    return ent.TB_MEDIDOR.Select(x => new REP_MEDIDORDISPLAY
    {
            Data_TOI = x.Data_TOI,
            Elemento = x.Elemento,
            Fase = x.Fase,
            KdKe = x.KD_KE,
            N_Equipamento = x.Numero,
            Tensao = x.Tensao,
            Status =  x.TB_REVISAO.Count > 0 ? "Revisão": 
                      x.TB_CHECAGEM_INTERNA.Count > 0 ? "Checagem interna":
                      x.TB_MESACALIBRACAO.Count > 0 ? "Mesa de calibração":
                      x.TB_HIPOT.Count > 0 ? "Hipot":
                      x.TB_INSPECAO.Count > 0? "Inspeção" :
                      x.TB_AGENDAMENTO.FirstOrDefault(y => y.ID_Medidor == x.ID).Data_Agendamento.HasValue ?
                  --> Error here (x.TB_AGENDAMENTO.FirstOrDefault(y => y.ID_Medidor == x.ID).Data_Agendamento.Value.ToString()) :String.Empty 
    }).ToList<REP_MEDIDORDISPLAY>();
}

But it's firing the following error when I try to convert a DateTime to String:

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

I hope you can help me guys, I need that value as string, I can't change that to datetime.

6
  • possible duplicate of Entity Framework Convert DateTime to string Commented Oct 18, 2013 at 13:15
  • I don't see how that would help me. my convert is inside a subquery. Commented Oct 18, 2013 at 13:18
  • If you need this then Put another column to the table containing the date as string. Or write a stored procedure to get the same result. Commented Oct 18, 2013 at 13:19
  • There's no way to convert that to string? if I need to add a new column to the Class I'll have to change a lot of things =/ Commented Oct 18, 2013 at 13:21
  • I am not on my computer to test this but why not use Data_Agendamento.Years + Data_Agendamento. Months + .... Commented Oct 18, 2013 at 13:32

2 Answers 2

4

Entity Framework does not know how to execute ToString() method in SQL. So you should load the data by using ToList() and then translate into SelectListItem as:

return ent.TB_MEDIDOR.ToList().Select(x => new SelectListItem
{ 
    Data_TOI = x.Data_TOI,
    ...
    // can convert DateTime to String here
})
// Then you can select this as a REP_MEDIDORDISPLAY if you want
.Select(y => new REP_MEDIDORDISPLAY
{
   Data_TOI = y.x.Data_TOI,
   ...
});
Sign up to request clarification or add additional context in comments.

1 Comment

Calling ToList where you are will create the full entity. It's probably better to call AsEnumerable after selecting the SelectListItem items, then finishing with the final select.
1

Remember that entity framework is converting your linq query to SQL. The error message is quite clear, it doesn't recognize the .ToString() method.

You will have to retrieve the results using entity framework and then perform the needed DateTime to String conversions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.