使用 var 匿名对象获取 LINQ 的 InvalidCastException
我在尝试使用 foreach 构造中的 var 变量循环查询对象时遇到错误。出于明确的原因,我将 xml 加载到数据集中并执行 Dataset to Linq。请纠正我的错误。这是我的代码:
ds.ReadXml(fsReadXml);
DataTable myDataTable = ds.Tables[0];
var ordersQuery = myDataTable.AsEnumerable();
var subQuery = (from Geo03 in ordersQuery
select new
{
PARENTTYPE = Geo03.Field<String>("PARENTTYPE"),
PARENTINSTANCE = Geo03.Field<int>("PARENTINSTANCE"),
CHILDTYPE = Geo03.Field<String>("CHILDTYPE"),
CHILDINSTANCE = Geo03.Field<String>("CHILDINSTANCE"),
CHILDPOS = Convert.ToInt32(Geo03.Field<int>("CHILDPOS"))
});
XDocument doc = null;
foreach (var cin in subQuery)
{
}
这是生成的错误:
System.InvalidCastException: Specified cast is not valid.
at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at SpectraWorkon.Program.<Main>b__0(DataRow Geo03) in c:\users\arun\documents\visual studio 2010\Projects\SpectraWorkon\SpectraWorkon\Program.cs:line 29
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at SpectraWorkon.Program.Main(String[] args) in c:\users\arun\documents\visual studio 2010\Projects\SpectraWorkon\SpectraWorkon\Program.cs:line 38
我收到 foreach (var cin in subQuery)
行错误,说它无法转换。
I am getting error while trying to loop through query object using var variable in foreach construct. I am loading xml in to dataset and performing Dataset to Linq for explicit reasons. Please correct my mistake. This is my code:
ds.ReadXml(fsReadXml);
DataTable myDataTable = ds.Tables[0];
var ordersQuery = myDataTable.AsEnumerable();
var subQuery = (from Geo03 in ordersQuery
select new
{
PARENTTYPE = Geo03.Field<String>("PARENTTYPE"),
PARENTINSTANCE = Geo03.Field<int>("PARENTINSTANCE"),
CHILDTYPE = Geo03.Field<String>("CHILDTYPE"),
CHILDINSTANCE = Geo03.Field<String>("CHILDINSTANCE"),
CHILDPOS = Convert.ToInt32(Geo03.Field<int>("CHILDPOS"))
});
XDocument doc = null;
foreach (var cin in subQuery)
{
}
This is the generated error:
System.InvalidCastException: Specified cast is not valid.
at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at SpectraWorkon.Program.<Main>b__0(DataRow Geo03) in c:\users\arun\documents\visual studio 2010\Projects\SpectraWorkon\SpectraWorkon\Program.cs:line 29
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at SpectraWorkon.Program.Main(String[] args) in c:\users\arun\documents\visual studio 2010\Projects\SpectraWorkon\SpectraWorkon\Program.cs:line 38
I am getting error for the line foreach (var cin in subQuery)
, saying it is unable to cast.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在对非
int
字段调用Field
。You're calling
Field<int>
on a field that isn't anint
.请记住,直到您实际使用 linq 查询(例如在 foreach 循环中,或者您调用 .ToList())时,它才会执行。因此,在您的情况下,例外是在您构建查询的部分。
InvaliCastException
表明您正在填写的字段不是您期望的类型。因此,我建议您检查查询中Field
的类型。Have in mind that the linq query isn't executed until you actually use it(for example in a foreach loop, or you call .ToList()). So in your case the exception is in the part where you build your query. The
InvaliCastException
suggests that you are filling a field that is not the type that you expect it to be. So I would suggest that you check the types of theField<T>
in your query.