参考列表功能混乱

时间:2017-06-16 05:48:47

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

您好我的代码基本问题是“这是一个不好的做法吗?”基本上我是从读取器功能读取数据并插入值已经存在列表(少数道具)我的代码是这样的

private void TGenerateDriveDetailsFromReader<T>(SqlDataReader returnData, ref List<Systems> systemList)
    {
        try
        {
            while (returnData.Read())
            {
                int MachineID = 0;
                MachineID = returnData["MachineID"] is DBNull ? 0 : (int)returnData["MachineID"];
                if (systemList.Any(x => x.ID == MachineID))
                {
                    double totalSize = returnData["Size"] is DBNull ? 0 : Convert.ToDouble(returnData["Size"]);
                    double freeSpace = returnData["FreeSpace"] is DBNull ? 0 : Convert.ToDouble(returnData["FreeSpace"]);
                    int driveType = returnData["DriveTypeID"] is DBNull ? 0 : (int)(returnData["DriveTypeID"]);
                    InsertDriveDetailsToList(totalSize, MachineID, freeSpace, driveType, ref systemList);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }




    private void InsertDriveDetailsToList(double totalSize, int machineID, double freeSpace, int driveTypeID, ref List<Systems> systemList)
    {
        switch (driveTypeID)
        {
            case 1: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveCTotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveCFree = freeSpace;
                break;

            case 2: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveDTotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveDFree = freeSpace;
                break;

            case 3: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveETotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveEFree = freeSpace;
                break;

            case 4: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveFTotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveCFree = freeSpace;
                break;

            case 5: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveGTotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveGFree = freeSpace;
                break;

            case 6: systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveHTotal = totalSize;
                systemList.Where(x => x.ID == machineID).FirstOrDefault().DriveHFree = freeSpace;
                break;
        }
    } 

所以带有case语句的函数每次都会执行,而read循环我的问题是因为我在循环中引用它也会影响性能吗?我应该改变方法吗?感谢

1 个答案:

答案 0 :(得分:0)

private void TGenerateDriveDetailsFromReader<T>(SqlDataReader returnData, ref List<Systems> systemList)
    {
        try
        {
            while (returnData.Read())
            {
                int MachineID = 0;
                MachineID = returnData["MachineID"] is DBNull ? 0 : (int)returnData["MachineID"];
                if (systemList.Any(x => x.ID == MachineID))
                {
                    double totalSize = returnData["Size"] is DBNull ? 0 : Convert.ToDouble(returnData["Size"]);
                    double freeSpace = returnData["FreeSpace"] is DBNull ? 0 : Convert.ToDouble(returnData["FreeSpace"]);
                    int driveType = returnData["DriveTypeID"] is DBNull ? 0 : (int)(returnData["DriveTypeID"]);
        Systems systems = systemList.Where(x => x.ID == machineID).FirstOrDefault();
                    InsertDriveDetailsToList(totalSize, MachineID, freeSpace, driveType, systems);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }




    private void InsertDriveDetailsToList(double totalSize, int machineID, double freeSpace, int driveTypeID, Systems systems)
    {
        switch (driveTypeID)
        {
            case 1: systems.DriveCTotal = totalSize;
                systems.DriveCFree = freeSpace;
                break;

            case 2: systems.DriveDTotal = totalSize;
                systems.DriveDFree = freeSpace;
                break;

            case 3: systems.DriveETotal = totalSize;
                systems.DriveEFree = freeSpace;
                break;

            case 4: systems.DriveFTotal = totalSize;
                systems.DriveCFree = freeSpace;
                break;

            case 5: systems.DriveGTotal = totalSize;
                systems.DriveGFree = freeSpace;
                break;

            case 6: systems.DriveHTotal = totalSize;
                systems.DriveHFree = freeSpace;
                break;
        }
    } 

您可以更改此内容。