无法将类型为“ <selectmanyiterator>”的对象转换为类型为“ System.Collections.Generic.List

时间:2018-09-12 03:58:39

标签: c# linq

我需要从列表中选择特定的两列,并为两个组合框加载这些值。我试图使用LINQ来做到这一点,它向我传递了一个错误“无法将类型'的对象强制转换为'System.Collections.Generic.List”。

这是我的方法,

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

这是车辆搜索方法,

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

2 个答案:

答案 0 :(得分:6)

此行是错误:

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

oData.SelectMany(x => x.VehicleNo)返回IEnumerable<char>,然后尝试将其强制转换为List<Exp_VehicleDTO>

应该是:

var vehicleNos = oData.Select(x => x.VehicleNo).ToList();

答案 1 :(得分:1)

我认为您需要使用Select()而不是SelectMany()。尝试将方法更改为此:

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