gpt4 book ai didi

c# - 引用列表功能困惑

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:08 26 4
gpt4 key购买 nike

您好,我对我的代码基本问题有疑问:“这是一种不好的做法吗?”基本上我正在从读取器函数读取数据并将值插入到已经存在的列表(几个 Prop )我的代码是这样的

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 语句的函数每次都会在 while read 循环中执行我的问题是因为我也在循环中引用列表会影响性能吗?我应该改变方法吗?谢谢

最佳答案

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

您可以更改它。

关于c# - 引用列表功能困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44581679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com