Cannot implicitly convert type system.Data.EnumerableRowCollectio to System.collection.Generic.Ienumerable

为君一笑 提交于 2021-01-28 08:12:40

问题


this is my code

public IEnumerable<TestUser> Getdata()
{
        //return new string[] { "value1", "value2" };
    TestUserBl tstusr = new TestUserBl();
    DataTable dt = new DataTable();
    dt = tstusr.TestUserSel();
    return dt.AsEnumerable();
}

getting error

Cannot implicitly convert type 'System.Data.EnumerableRowCollection' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)


回答1:


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
                             });
}


来源:https://stackoverflow.com/questions/44321517/cannot-implicitly-convert-type-system-data-enumerablerowcollectio-to-system-coll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!