Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a problem with a conversion from linq to generic object, and it just fails here, I've others linqs querys and they work just fine.

I don't find where the bug is and I've tried several ways to do the same. The code that is failing is this:

SQL
var queryT = from proc in DataContext.Procedimentos
                                 join ass in DataContext.Assistencias on proc.assistencia equals ass.codigo
                                 join mac in DataContext.Maquinas on ass.maquina equals mac.SN
                                 where ass.maquina == maq.SN
                                 select proc;

C#
foreach (object procce in queryT)
                            {
                                Procedimento proccess = (Procedimento)procce;
                                TimeSpan span = proccess.fim.Subtract(proccess.inicio);
                                tempo = tempo + span.TotalHours;
                            }


throws InvalidCastException in the foreach instrution

but when in debug mode I write in the watch (object)queryT.First() and it works
Posted

Ok, found the problem, the problem was not in that code, but in the SQL table, one field had a different type from the class Procedimento...

Everything works now
 
Share this answer
 
Try this:

List<object> queryT = null;
queryT = (from proc in DataContext.Procedimentos
          join ass in DataContext.Assistencias on proc.assistencia equals ass.codigo
          join mac in DataContext.Maquinas on ass.maquina equals mac.SN
          where ass.maquina == maq.SN
          select proc).ToList();

foreach...</object>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900