将Linq转换为Object到泛型对象的问题 [英] Convertin Linq to Object to a generic object problems
本文介绍了将Linq转换为Object到泛型对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在从linq转换为通用对象时遇到问题,但是在这里失败了,我还有其他linqs查询,它们工作得很好.
我找不到错误的位置,并且我尝试了几种方法来完成此操作.失败的代码是这样的:
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:
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;
foreach (object procce in queryT)
{
Procedimento proccess = (Procedimento)procce;
TimeSpan span = proccess.fim.Subtract(proccess.inicio);
tempo = tempo + span.TotalHours;
}
在foreach指令中引发InvalidCastException
但是在调试模式下,我在监视(object)queryT.First()中编写了它,并且工作正常
throws InvalidCastException in the foreach instrution
but when in debug mode I write in the watch (object)queryT.First() and it works
推荐答案
好,发现了问题,问题出在那代码,但是在SQL表中,一个字段的类型不同于Procedimento类.
现在一切正常
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
尝试一下:
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>
这篇关于将Linq转换为Object到泛型对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文