public Dictionary<string, List<BasicEntity>> ValidateProcess(string excelFile) { var readExcelResp = excelReader.ReadExcel(excelFile); foreach (var keyValuePair in readExcelResp) { var errors = new List<BasicEntity>(); foreach (var basicEntity in keyValuePair.Value) { var validationResult = ExecuteValidate(basicEntity); if (validationResult != null && !validationResult.IsValid) { errors.Add(basicEntity); foreach (var validationResultError in validationResult.Errors) { excelErrorLogService.AddErrorLog(basicEntity.MigrationRowNum.ToString(), validationResultError.ErrorMessage, keyValuePair.Key); } } } if (errors.Any()) { keyValuePair.Value.RemoveAll(i => errors.Contains(i)); } if (keyValuePair.Value.Any()) { var verifySet = new HashSet<string>(); var sameList = new List<BasicEntity>(); var identity = keyValuePair.Value[0].GetIdentity(); if (!string.IsNullOrEmpty(identity)) { var fields = identity.Split(""); foreach (var basicEntity in keyValuePair.Value) { List<string> fieldsValue = new(); foreach (var field in fields) { fieldsValue.Add(basicEntity.GetType() .GetField(field, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(basicEntity) ?.ToString()); } var values = string.Join("", fieldsValue); if (verifySet.Contains(values)) { sameList.Add(basicEntity); _excelErrorLogService.AddErrorLog(basicEntity.MigrationRowNum.ToString(), $"Fields {fields} values repetition" , keyValuePair.Key); } else { verifySet.Add(values); } } } } } return readExcelResp; } 优化这段代码
时间: 2024-03-28 18:41:42 浏览: 49
Unity读取Excel文件转换XML格式文件
这段代码可以进行以下优化:
1. 使用 LINQ 来简化代码,例如使用 `Where` 和 `Select` 来筛选和映射集合,使用 `Any` 和 `All` 来判断集合是否为空或满足条件。
2. 将重复的代码抽象成方法或者变量,避免代码重复。
3. 使用 `StringBuilder` 来拼接字符串,避免大量的字符串连接操作。
下面是优化后的代码:
```
public Dictionary<string, List<BasicEntity>> ValidateProcess(string excelFile)
{
var readExcelResp = excelReader.ReadExcel(excelFile);
foreach (var keyValuePair in readExcelResp)
{
var errors = keyValuePair.Value
.Select(ExecuteValidate)
.Where(validationResult => validationResult != null && !validationResult.IsValid)
.SelectMany(validationResult => validationResult.Errors.Select(error => (validationResult.BasicEntity, error)))
.ToList();
errors.ForEach(error => _excelErrorLogService.AddErrorLog(error.BasicEntity.MigrationRowNum.ToString(), error.ErrorMessage, keyValuePair.Key));
keyValuePair.Value.RemoveAll(errors.Select(error => error.BasicEntity).Contains);
if (keyValuePair.Value.Any())
{
var identityFields = keyValuePair.Value.FirstOrDefault()?.GetIdentity()?.Split(";");
if (identityFields != null)
{
var sameList = new List<BasicEntity>();
var verifySet = new HashSet<string>();
foreach (var basicEntity in keyValuePair.Value)
{
var values = identityFields.Select(field => basicEntity.GetType().GetField(field, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(basicEntity)?.ToString()).Aggregate(new StringBuilder(), (sb, value) => sb.Append(value)).ToString();
if (verifySet.Contains(values))
{
sameList.Add(basicEntity);
_excelErrorLogService.AddErrorLog(basicEntity.MigrationRowNum.ToString(), $"Fields {string.Join(",", identityFields)} values repetition", keyValuePair.Key);
}
else
{
verifySet.Add(values);
}
}
keyValuePair.Value.RemoveAll(sameList.Contains);
}
}
}
return readExcelResp;
}
```
阅读全文