这个函数需要很多时间才能得到结果,我认为这里有问题。任何帮助来重构它?
我正在使用实体框架和通用工作单元模式,我有 3 个表 RosConfig
,Procedure
和RosConfigProcedure
多关系。
我想要的是获取所有RosConfigs每个对象和他的过程对象,并将其映射到viewModel对象"RosConfigsJson"
private List<RosConfigsJson> GetAllRosConfigs()
{
List<RosConfigsJson> rosConfigsJsons = new List<RosConfigsJson>();
var rosConfigs = _unitOfWork.RosConfigRepository.Get().ToList();
foreach (var rosConfig in rosConfigs)
{
var roscj = new RosConfigsJson
{
RosConfig = rosConfig,
ProceduresJsons = GetProcByRosConfigs(rosConfig.RosConfigId)
};
rosConfigsJsons.Add(roscj);
}
return rosConfigsJsons;
}
private List<ProceduresJson> GetProcByRosConfigs(int rosConfigId)
{
var listOfProceduresIds = _unitOfWork.RosConfigProcedureRepository
.Get(r => r.RosConfigId == rosConfigId)
.Select(c=>c.ProcedureId);
var procJson = _unitOfWork.ProceduresRepository.
Get(p => listOfProceduresIds.Contains(p.ProcedureId)).
Select(p=> new ProceduresJson{Id = p.ProcedureId , Name = p.Name}) .ToList();
return procJson;
}
视图模型类
class RosConfigsJson
{
public List<ProceduresJson> ProceduresJsons { get; set; }
public RosConfig RosConfig { get; set; }
}
class ProceduresJson
{
public int Id { get; set; }
public string Name { get; set; }
}
实体
[Table("Procedures")]
public class Procedures
{
public Procedures()
{
ProcedureCodes = new List<ProcedureCode>();
}
[Key]
public int ProcedureId { get; set; }
public int? ProcedureNoteId { get; set; }
public int? MedicalBestPracticeId { get; set; }
public string Name { get; set; }
public string CptCode { get; set; }
public bool IsActive { get; set; }
[JsonIgnore]
public virtual ICollection<ProcedureCode> ProcedureCodes { get; set; }
[ForeignKey("ProcedureNoteId")]
public virtual ProcedureNote ProcedureNote { get; set; }
[ForeignKey("MedicalBestPracticeId")]
public virtual MedicalBestPractice MedicalBestPractice { get; set; }
}
[Table("RosConfig")]
public class RosConfig
{
public RosConfig()
{
RosConfigProcedures = new List<RosConfigProcedure>();
}
[Key]
public int RosConfigId { get; set; }
public string RosName { get; set; }
public int RosIndex { get; set; }
public int? RosSectionId { get; set; }
[JsonIgnore]
public virtual ICollection<RosConfigProcedure> RosConfigProcedures { get; set; }
[ForeignKey("RosSectionId")]
public virtual RosConfigSection RosConfigSection { get; set; }
}
[Table("RosConfigProcedure")]
public class RosConfigProcedure
{
[Key]
public int RosConfigProcedureId { get; set; }
public int RosConfigId { get; set; }
public int ProcedureId { get; set; }
[ForeignKey("RosConfigId")]
public virtual RosConfig RosConfig { get; set; }
[ForeignKey("ProcedureId")]
public virtual Procedures Procedures { get; set; }
}
Genirc UnitOfWork 上的 get 方法
// The code Expression<Func<TEntity, bool>> filter means
//the caller will provide a lambda expression based on the TEntity type,
//and this expression will return a Boolean value.
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = DbSet;
if (filter != null)
{
query = query.AsExpandable().Where(filter);
}
// applies the eager-loading expressions after parsing the comma-delimited list
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
没有List<T>().Get()
这样的东西,使用.Where()