将数据表转换为IEnumerable

时间:2015-11-07 10:35:07

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我收到以下错误

  

错误: - 无法隐式转换类型   ' System.Data.EnumerableRowCollection'至   ' System.Collections.Generic.IEnumerable&#39 ;.   存在显式转换(你错过了吗?   cast?)C:\ Users \ e19206 \ Documents \ Visual Studio   2010 \ Projects \ TerroristMVC \ TerroristBAL \ Models \ Landingbal.cs 51 20 TerroristBAL

这是我的代码

public IEnumerable<Landing> getReportSummary()
{
    DataTable dtRepSummary = new DataTable();

    try
    {
        procname = "TDS_QUERY.GET_REP_SUMMARY";

        OracleParameter[] oraParam = new OracleParameter[1];

        oraParam[0] = new OracleParameter("TDS_RECORDSET", OracleDbType.RefCursor);
        oraParam[0].Direction = ParameterDirection.Output;

        connection = new OracleConnection(SqlHelper.GetConnection());

        DataSet ds = SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, procname, oraParam);

        dtRepSummary = ds.Tables[0];
    }
    catch (Exception ex)
    {
        string errormessage = "Method name:- " + MethodBase.GetCurrentMethod().Name + " | Description: " + ex.Message + " " + ex.InnerException;
        log.Error(errormessage);
    }

    return dtRepSummary.AsEnumerable();
}

这是我的登陆模型类

public class Landing
{
    #region properties

    public string Reportid { get; set; }

    public string Reportdate { get; set; }

    public string Fromdate { get; set; }

    public string Todate { get; set; }

    public string Executed { get; set; }

    public string Userid { get; set; }

    public string Recadddate { get; set; }

    #endregion
} 

1 个答案:

答案 0 :(得分:1)

您收到该错误是因为dtRepSummary.AsEnumerable()返回IEnumerable<DataRow>但您的返回类型为IEnumerable<Landing>,您可以使用Select投影您的类型: - < / p>

return dtRepSummary.AsEnumerable().Select(x => new Landing
                                        {
                                           Reportid = x.Field<int>("Reportid"), 
                                           Reportdate = x.Field<string>("Reportdate"),
                                           ..and so on
                                        };