如何重构此LINQ查询

时间:2015-06-12 15:44:04

标签: c# linq refactoring asp.net-web-api2 unit-of-work

这个功能花了很多时间才能得到结果,我觉得这里有点不对劲。 有什么帮助可以重构它吗?

我使用实体框架和通用unitOfWork Pattern,我有3个表 <{1}},RosConfigProcedure,用于多对多关系。

我想要的是使用他的Procedures对象获取所有RosConfigs的每个对象并将其映射到viewModel对象&#34; RosConfigsJson&#34;

RosConfigProcedure

ViewModel Classes

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; }

}

Genirc UnitOfWork上的获取方法

[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; }

    }

1 个答案:

答案 0 :(得分:-3)

没有List<T>().Get()这样的东西,请使用.Where()