0

i need to select specific two columns from a list and load those values for two combo boxes. i tried to do this using LINQ and it is passing me an error " Unable to cast object of type ' to type 'System.Collections.Generic.List ".

Here is my method,

private void getVehicleNo()
{
    List<Exp_VehicleDTO> oData = oVehicleBL.VehiclesSearch(Convert.ToInt32(Session["CompanyID"].ToString()));
    oData = ((List<Exp_VehicleDTO>)oData.SelectMany(x => x.VehicleNo)).ToList();
    ddVehicleNo.DataSource = oData;
    ddVehicleNo.DataBind();
}

here is the vehicle search method,

public List<Exp_VehicleDTO> VehiclesSearch(int companyId)
{
    List<Exp_VehicleDTO> results = new List<Exp_VehicleDTO>();
    try
    {
        using (CloudConnection oCloudConnection = new CloudConnection(DMSSWE.Common.ConnectionString))
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(" SELECT ");
            sb.AppendLine("CompanyID,");
            sb.AppendLine("VehicleId,");
            sb.AppendLine("VehicleType,");
            sb.AppendLine("VehicleNo,");
            sb.AppendLine("Status,");
            sb.AppendLine("CreatedDateTime,");
            sb.AppendLine("CreatedBy,");
            sb.AppendLine("CreatedMachine ");
            sb.AppendLine(" FROM Exp_CompanyVehicle ");
            sb.AppendLine(" WHERE 1=1 ");
            sb.AppendLine(" AND (CompanyID=?CompanyID)");

            oCloudConnection.CommandText = sb.ToString();
            oCloudConnection.Parameters.Clear();
            oCloudConnection.Parameters.Add(new Parameter { Name = "CompanyID", Value = companyId });

            using (IDataReader dr = oCloudConnection.ExecuteReader())
            {
                while (dr.Read())
                {
                    Exp_VehicleDTO result = new Exp_VehicleDTO();
                    result.CompanyId = Helper.GetDataValue<int>(dr, "CompanyID");
                    result.VehicleId = Helper.GetDataValue<int>(dr, "VehicleId");
                    result.VehicleType = Helper.GetDataValue<int>(dr, "VehicleType");
                    result.VehicleNo = Helper.GetDataValue<string>(dr, "VehicleNo");
                    result.Status = Helper.GetDataValue<int>(dr, "Status");
                    result.CreatedDateTime = Helper.GetDataValue<DateTime>(dr, "CreatedDateTime");
                    result.CreatedBy = Helper.GetDataValue<string>(dr, "CreatedBy");
                    result.CreatedMachine = Helper.GetDataValue<string>(dr, "CreatedMachine");
                    results.Add(result);
                }
                dr.Close();
            }
        }
        return results;
    }
    catch (Exception ex)
    {
        Logger.Write(ex);
        throw ex;
    }
}
Jonas W
  • 3,200
  • 1
  • 31
  • 44
Didu
  • 79
  • 2
  • 11

2 Answers2

7

This line is the error :

((List<Exp_VehicleDTO>)oData.SelectMany(x => x.VehicleNo)).ToList()

oData.SelectMany(x => x.VehicleNo) returns IEnumerable<char> then you try to cast it to List<Exp_VehicleDTO>

Should be :

var vehicleNos = oData.Select(x => x.VehicleNo).ToList();
Jonas W
  • 3,200
  • 1
  • 31
  • 44
1

I think you need to use Select() not SelectMany(). Try changing your method to this:

private void getVehicleNo()
{
    List<Exp_VehicleDTO> oData = oVehicleBL.VehiclesSearch(Convert.ToInt32(Session["CompanyID"].ToString()));
    List<string> vehicleNoList = oData.Select(x => x.VehicleNo).ToList();
    ddVehicleNo.DataSource = vehicleNoList;
    ddVehicleNo.DataBind();
}
Jesse Johnson
  • 1,638
  • 15
  • 25