coder-github578
coder-github578

Reputation: 103

Error Converting Datatable to IEnumerable

I am getting the following error

Error :- Cannot implicitly convert type 'System.Data.EnumerableRowCollection' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) C:\Users\e19206\Documents\Visual Studio 2010\Projects\TerroristMVC\TerroristBAL\Models\Landingbal.cs 51 20 TerroristBAL

Here is my code

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

Here is my landing Model class

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
} 

Upvotes: 0

Views: 2222

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21825

You are getting that error because dtRepSummary.AsEnumerable() returns IEnumerable<DataRow> but you have the return type as IEnumerable<Landing>, you can use Select to project your type like this:-

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

Upvotes: 1

Related Questions