How to refactor this LINQ query

58 views Asked by At

This function is taking to much time to get results and i think is something wrong here. Any Help to refactor it ?

i'm using entity framework and Generic unitOfWork Pattern , i have 3 tables RosConfig , Procedure and RosConfigProcedure for many to many relation.

what i want is Get All RosConfigs every object with his Procedures objects and map it to viewModel object "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;
}

ViewModel Classes

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

}

Entities

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

    }

The Get Method on the Genirc UnitOfWork

// 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();
    }
}
1

There are 1 answers

1
maraaaaaaaa On

There's no such thing as List<T>().Get(), use .Where()