如何仅使用用户提供输入的变量来搜索对象数组
How to search through an array of objects using only variables that user provided input for
马上我要说 - 我是一个认真的初学者,这是我的学习项目。
我正在尝试制作一种管理员可以搜索满足特定条件的帐户的方法。
首先,系统会提示他输入所有参数,然后我只想使用具有一些输入的参数来搜索满足所有条件的帐户。
如果所有参数都有一些输入,这是它搜索数组的部分:
for (int index = 0; index < objAccount.Length; index++)
{
if (objAccount[index].accNum == accNum && objAccount[index].accLogin == accLogin && objAccount[index].accName == accName && objAccount[index].accBalance == accBalance && objAccount[index].accType == accType && objAccount[index].accStatus == accStatus)
{
Console.WriteLine($"{objAccount[index].accNum,15}" + $"{objAccount[index].accLogin,15}" + $"{objAccount[index].accName,20}" + $"{objAccount[index].accBalance,15:C}" + $"{objAccount[index].accType,15}" + $"{objAccount[index].accStatus,15}");
}
}
以我有限的知识,我想出的一个解决方案是对所有参数执行 if/else ifs,但由于我必须对所有组合执行此操作,因此有很多代码似乎没有必要。肯定有一种我只是没有看到的更有效的方法来做到这一点。
谁能帮我解决这个问题?
我会这样做:
(您必须根据数据类型调整每行的第一部分(空检查))
var filtered = objAccount.Where( x =>
(accNum == null || x.accNum == accNum) &&
(accLogin == null || x.accLogin == accLogin) &&
(String.IsNullOrEmpty(accName) || x.accName == accName) &&
(accBalance == null || x.accBalance == accBalance) &&
(accType == null || x.accType == accType) &&
(accStatus == null || x.accStatus == accStatus)
);
foreach (var item in filtered)
{
Console.WriteLine ...
}
你仍然可以使用 foreach 来避免双重迭代
foreach (var item in objAccount)
{
if (
(accNum == null || item.accNum == accNum) &&
(accLogin == null || item.accLogin == accLogin) &&
(string.IsNullOrEmpty(accName) || item.accName == accName) &&
(accBalance == null || item.accBalance == accBalance) &&
(accType == null || item.accType == accType) &&
(accStatus == null || item.accStatus == accStatus)
)
Console.WriteLine($" {item.accNum,15} {item.accLogin,15} {item.accName,20} { item.accBalance,15:C}{ item.accType,15}{ item.accStatus,15}");
}
马上我要说 - 我是一个认真的初学者,这是我的学习项目。 我正在尝试制作一种管理员可以搜索满足特定条件的帐户的方法。 首先,系统会提示他输入所有参数,然后我只想使用具有一些输入的参数来搜索满足所有条件的帐户。
如果所有参数都有一些输入,这是它搜索数组的部分:
for (int index = 0; index < objAccount.Length; index++)
{
if (objAccount[index].accNum == accNum && objAccount[index].accLogin == accLogin && objAccount[index].accName == accName && objAccount[index].accBalance == accBalance && objAccount[index].accType == accType && objAccount[index].accStatus == accStatus)
{
Console.WriteLine($"{objAccount[index].accNum,15}" + $"{objAccount[index].accLogin,15}" + $"{objAccount[index].accName,20}" + $"{objAccount[index].accBalance,15:C}" + $"{objAccount[index].accType,15}" + $"{objAccount[index].accStatus,15}");
}
}
以我有限的知识,我想出的一个解决方案是对所有参数执行 if/else ifs,但由于我必须对所有组合执行此操作,因此有很多代码似乎没有必要。肯定有一种我只是没有看到的更有效的方法来做到这一点。
谁能帮我解决这个问题?
我会这样做:
(您必须根据数据类型调整每行的第一部分(空检查))
var filtered = objAccount.Where( x =>
(accNum == null || x.accNum == accNum) &&
(accLogin == null || x.accLogin == accLogin) &&
(String.IsNullOrEmpty(accName) || x.accName == accName) &&
(accBalance == null || x.accBalance == accBalance) &&
(accType == null || x.accType == accType) &&
(accStatus == null || x.accStatus == accStatus)
);
foreach (var item in filtered)
{
Console.WriteLine ...
}
你仍然可以使用 foreach 来避免双重迭代
foreach (var item in objAccount)
{
if (
(accNum == null || item.accNum == accNum) &&
(accLogin == null || item.accLogin == accLogin) &&
(string.IsNullOrEmpty(accName) || item.accName == accName) &&
(accBalance == null || item.accBalance == accBalance) &&
(accType == null || item.accType == accType) &&
(accStatus == null || item.accStatus == accStatus)
)
Console.WriteLine($" {item.accNum,15} {item.accLogin,15} {item.accName,20} { item.accBalance,15:C}{ item.accType,15}{ item.accStatus,15}");
}