【问题标题】:Cannot implicitly convert type system.Data.EnumerableRowCollectio to System.collection.Generic.Ienumerable无法将类型 system.Data.EnumerableRowCollectio 隐式转换为 System.collection.Generic.Ienumerable
【发布时间】:2017-11-03 10:15:45
【问题描述】:

这是我的代码

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'。 存在显式转换(您是否缺少演员表?)

【问题讨论】:

  • TestUserSel() 方法有什么作用?

标签: c# asp.net-web-api


【解决方案1】:

实际上DataTable.AsEnumerable() 为您提供EnumerableRowCollection,如错误消息所述,如果您需要获取特定对象集合,则必须获取该对象,如下所示:

public IEnumerable<TestUser> Getdata()
{
    // Code here
    return dt.AsEnumerable().Select(x => new TestUser{
                              someId = x.Field<string>("id"),
                             // like wise initialize properties here
                             });
}

【讨论】:

  • 我只是没有在函数中传递任何值。所以这个 someId = x.Field("id"), 有什么用
  • @AkhilGeorge:那将是 DataTable 的列名,dt。您必须将每列映射/分配给类的相应属性'
  • 这给了我 w3.org/2001/XMLSchema-instance" xmlns="schemas.datacontract.org/2004/07/StarAgentWebApi.Models"> 1 tu_id> Akhil
  • 如何将此文件类型转换为json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 2022-01-15
相关资源
最近更新 更多