Unable to cast object of type 'System.Collections.Generic.List

16,433

Solution 1

You need to cast Session["TripRecords"] to List<TransTripLeg> instead of DataTable

var collection = (List<TransTripLeg>)Session["TripRecords"];
var legs = collection.Where(c => c.LegID == sLegID);

Solution 2

Remove this symbol just works for me(') this works fine for me

DataRow[] dr = allleaveDt.Select("EmployeeId=" + empId);
Share:
16,433
Elsa
Author by

Elsa

(csharpnewbie) The more I learn .Net, the more there is to learn....

Updated on June 05, 2022

Comments

  • Elsa
    Elsa over 1 year

    I am getting this error

    Unable to cast object of type 'System.Collections.Generic.List`1[TransTripLeg]' to type 'System.Data.DataTable'.

    and I can't seem to figure out how to resolve it. The error occurs at DataTable dtTable = in the code below.

    if (!string.IsNullOrEmpty(Request.QueryString["EditMode"]))
    {
        DataTable dtUpdate = (DataTable)Session["TripRecords"];
        DataRow[] customerRow = dtUpdate.Select("LegID = '" + sLegID.ToString() + "'");
    

    I think I am casting it to type DataTable, but then I still get the error.