ProgramModelEntities = _migrateRepository.GetAllWithNoTrackingAsync<ProgramModelEntity>().ToList(); Programmes = ProgramModelEntities.Select(program => { CProgramme p = new CProgramme() { Id = new Guid(program.Id), Name = program.Name, SchoolId = SQIConstants.SYS_School_ID, Description = program.Description, CreatedById = String.IsNullOrEmpty(program.CreatedBy) ? new Guid("e306ca07-a21b-40fd-935a-343cfe369001") : new Guid(program.CreatedBy), CreatedTime = program.CreatedOn == null ? new DateTime().Ticks : program.CreatedOn.Value.Ticks, ModifiedById = String.IsNullOrEmpty(program.ModifiedBy) ? new Guid("e306ca07-a21b-40fd-935a-343cfe369001") : new Guid(program.ModifiedBy), ModifiedTime = program.ModifiedOn == null ? new DateTime().Ticks : program.ModifiedOn.Value.Ticks, Duration = 1 }; if (!String.IsNullOrEmpty(program.ProgramAdmins)) { var admins = program.ProgramAdmins.Split(";"); ResourceAccesses.Add(admins.Select(admin => new CResourceAccess() { Id = Guid.NewGuid(), UserId = new Guid(admin), RoleType = RoleType.ProgrammeAdmin, ResourceId = new Guid(program.Id), ResourceLevel = ResourceLevel.Programme } ).ToList()); } ResourcesList.Add(new CResources { Id = new Guid(program.Id), ResourceLevel = ResourceLevel.Programme, SchoolId = SQIConstants.SYS_School_ID, ProgrammeId = new Guid(program.Id), }); return p; } ).ToList(); 优化下这段代码
时间: 2024-02-10 19:14:42 浏览: 67
Matlab_note2.zip_matlab_vowelnoo_zip
以下是对这段代码的一些优化建议:
1. 使用异步方法来获取数据,可以提高性能。例如,可以使用 `ToListAsync()` 替代 `ToList()`。
2. 使用 LINQ 的 Select 方法来进行对象的转换,可以让代码更简洁易读。例如,可以使用如下的代码替代 `ProgramModelEntities.Select()`:
```
Programmes = await _migrateRepository.GetAllWithNoTrackingAsync<ProgramModelEntity>()
.Select(program => new CProgramme
{
Id = new Guid(program.Id),
Name = program.Name,
SchoolId = SQIConstants.SYS_School_ID,
Description = program.Description,
CreatedById = String.IsNullOrEmpty(program.CreatedBy)
? new Guid("e306ca07-a21b-40fd-935a-343cfe369001")
: new Guid(program.CreatedBy),
CreatedTime = program.CreatedOn?.Ticks ?? new DateTime().Ticks,
ModifiedById = String.IsNullOrEmpty(program.ModifiedBy)
? new Guid("e306ca07-a21b-40fd-935a-343cfe369001")
: new Guid(program.ModifiedBy),
ModifiedTime = program.ModifiedOn?.Ticks ?? new DateTime().Ticks,
Duration = 1
})
.ToListAsync();
```
3. 对于资源列表和资源访问列表的操作,可以使用 LINQ 的 SelectMany 方法和 Where 方法来替代 foreach 循环。例如,可以使用如下的代码替代 `if (!String.IsNullOrEmpty(program.ProgramAdmins))` 和 `ResourcesList.Add()`:
```
var programmeAdmins = program.ProgramAdmins?.Split(';');
if (programmeAdmins?.Any() == true)
{
ResourceAccesses.AddRange(programmeAdmins
.Select(admin => new CResourceAccess
{
Id = Guid.NewGuid(),
UserId = new Guid(admin),
RoleType = RoleType.ProgrammeAdmin,
ResourceId = new Guid(program.Id),
ResourceLevel = ResourceLevel.Programme
}));
}
ResourcesList.AddRange(programmeAdmins?.Select(admin => new CResources
{
Id = new Guid(program.Id),
ResourceLevel = ResourceLevel.Programme,
SchoolId = SQIConstants.SYS_School_ID,
ProgrammeId = new Guid(program.Id),
}) ?? Enumerable.Empty<CResources>());
```
综上所述,以下是经过优化后的代码:
```
Programmes = await _migrateRepository.GetAllWithNoTrackingAsync<ProgramModelEntity>()
.Select(program => new CProgramme
{
Id = new Guid(program.Id),
Name = program.Name,
SchoolId = SQIConstants.SYS_School_ID,
Description = program.Description,
CreatedById = String.IsNullOrEmpty(program.CreatedBy)
? new Guid("e306ca07-a21b-40fd-935a-343cfe369001")
: new Guid(program.CreatedBy),
CreatedTime = program.CreatedOn?.Ticks ?? new DateTime().Ticks,
ModifiedById = String.IsNullOrEmpty(program.ModifiedBy)
? new Guid("e306ca07-a21b-40fd-935a-343cfe369001")
: new Guid(program.ModifiedBy),
ModifiedTime = program.ModifiedOn?.Ticks ?? new DateTime().Ticks,
Duration = 1
})
.ToListAsync();
var programmeResources = Programmes.SelectMany(programme => new[]
{
new CResources
{
Id = programme.Id,
ResourceLevel = ResourceLevel.Programme,
SchoolId = SQIConstants.SYS_School_ID,
ProgrammeId = programme.Id
}
});
var programmeAdminResources = Programmes.SelectMany(programme =>
{
var programmeAdmins = programme.ProgramAdmins?.Split(';');
return programmeAdmins?.Select(admin => new CResourceAccess
{
Id = Guid.NewGuid(),
UserId = new Guid(admin),
RoleType = RoleType.ProgrammeAdmin,
ResourceId = programme.Id,
ResourceLevel = ResourceLevel.Programme
}) ?? Enumerable.Empty<CResourceAccess>();
});
ResourcesList.AddRange(programmeResources);
ResourceAccesses.AddRange(programmeAdminResources);
```
阅读全文