无法将类型system.Data.EnumerableRowCollectio隐式转换为System.collection.Generic.Ienumerable [英] Cannot implicitly convert type system.Data.EnumerableRowCollectio to System.collection.Generic.Ienumerable
本文介绍了无法将类型system.Data.EnumerableRowCollectio隐式转换为System.collection.Generic.Ienumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
public IEnumerable<TestUser> Getdata()
{
//return new string[] { "value1", "value2" };
TestUserBl tstusr = new TestUserBl();
DataTable dt = new DataTable();
dt = tstusr.TestUserSel();
return dt.AsEnumerable();
}
获取错误
无法隐式转换类型'System.Data.EnumerableRowCollection'到'System.Collections.Generic.IEnumerable'.存在显式转换(您是否缺少演员表?)
Cannot implicitly convert type 'System.Data.EnumerableRowCollection' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
推荐答案
实际上 DataTable.AsEnumerable()
给您 EnumerableRowCollection
作为错误消息,如果需要的话获取特定的对象集合,您必须像下面这样获取该对象:
Actually DataTable.AsEnumerable()
gives you EnumerableRowCollection
as the error message stated, if you need to get the specific object collection you have to get that object like the following:
public IEnumerable<TestUser> Getdata()
{
// Code here
return dt.AsEnumerable().Select(x => new TestUser{
someId = x.Field<string>("id"),
// like wise initialize properties here
});
}
这篇关于无法将类型system.Data.EnumerableRowCollectio隐式转换为System.collection.Generic.Ienumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文