问题
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