可以编写这样的代码,但不需要重复用户异常过滤器。
Array.Sort
答案 0 :(得分:3)
您可以将Any(filter)
+ First(filter)
替换为FirstOrDefault(filter)
+ null
支票,以避免重复过滤(并提高效果):
foreach (var val in users)
{
var match = val.FirstOrDefault(x =>
x.UserException.Contains("QPZ") || x.UserException.Contains("QPR"));
if (match != null)
{
listUsers.Add(match);
}
else
{
listUsers.AddRange(val);
}
}
答案 1 :(得分:0)
您可以使用例外列表并对其进行检查:
var exceptionList = new List<string> {
"QPZ",
"QPR"
};
foreach (var val in users)
{
if (val.Any(x => exceptionList.Contains(x.UserException))
{
listUsers.Add(
val?.First(s => exceptionList.Contains(x.UserException))
);
}
else
{
listUsers.AddRange(val);
}
}
答案 2 :(得分:0)
感谢Ivan和JanR,我找到了问题的答案,非常感谢你!
var exceptionList = new List<string> {"QPZ","QPR"};
foreach (var val in users)
{
var match = val.FirstOrDefault(x => exceptionList.Contains(x.UserException));
if (match != null)
{
usersList.Add(match);
}
else
{
usersList.AddRange(val);
}
}