首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重构此LINQ查询

如何重构此LINQ查询
EN

Stack Overflow用户
提问于 2015-06-12 23:44:04
回答 1查看 50关注 0票数 0

这个函数需要很长时间才能得到结果,我认为这里有问题。有没有帮助重构它?

我使用实体框架和通用unitOfWork模式,我有3个表RosConfigProcedureRosConfigProcedure用于多对多关系。

我想要的是用他的过程对象获取所有的RosConfigs每个对象,并将它映射到viewModel对象"RosConfigsJson“

代码语言:javascript
复制
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类

代码语言:javascript
复制
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; }

}

实体

代码语言:javascript
复制
[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; }

    }

Get方法在通用UnitOfWork上的实现

代码语言:javascript
复制
// 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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-06-12 23:53:02

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

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30807137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档