.Net实体两个可选参数

时间:2018-08-22 12:33:08

标签: .net entity-framework-4

我是.Net Entity框架的新手,正在寻找一种编写可以包含两个可选参数的函数的方法。有更好的方法吗?

public IQueryable<Template> GetTemplateAgentKeyDiseaseId(Guid agentKey, Guid diseaseId)
    {
        if (diseaseId != null)
        {
            return from t in UnitOfWork.GetRepository<Template>().Get()
                   join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                   join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                   join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                   join a in UnitOfWork.GetRepository<Agent>().Get() on re.AgentVersionKey equals a.VersionKey
                   where a.VersionKey == agentKey && t.IsCurrentVersion
                   select t;
        }
        else if (agentKey != null) 
        {

            return from t in UnitOfWork.GetRepository<Template>().Get()
                   join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                   join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                   join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                   join a in UnitOfWork.GetRepository<Agent>().Get() on re.AgentVersionKey equals a.VersionKey
                   where t.IsCurrentVersion && t.ExternalDiseaseId == diseaseId
                   select t;
        }else
        {
            return from t in UnitOfWork.GetRepository<Template>().Get()
                   join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                   join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                   join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                   join a in UnitOfWork.GetRepository<Agent>().Get() on re.AgentVersionKey equals a.VersionKey
                   where a.VersionKey == agentKey && t.IsCurrentVersion && t.ExternalDiseaseId == diseaseId
                   select t;
        }

1 个答案:

答案 0 :(得分:0)

  1. 您将if if (diseaseId != null)撤消了
  2. Guid是结构体,因此永远不会为空,请使用Guid?

您可以提取公共部分:

        public IQueryable<Template> GetTemplateAgentKeyDiseaseId(Guid? agentKey, Guid? diseaseId)
        {
            //Common part
            var query = from t in UnitOfWork.GetRepository<Template>().Get()
                        join r in UnitOfWork.GetRepository<Regimen>().Get() on t.Id equals r.TemplateId
                        join rp in UnitOfWork.GetRepository<RegimenPart>().Get() on r.Id equals rp.RegimenId
                        join re in UnitOfWork.GetRepository<RegimenEntry>().Get() on rp.Id equals re.RegimenPartId
                        join a in UnitOfWork.GetRepository<Agent>().Get() on re.AgentVersionKey equals a.VersionKey
                        where t.IsCurrentVersion
                        select new
                        {
                            T = t,
                            VersionKey = a.VersionKey,
                        };

            //Extra filters
            if (agentKey != null)
            {
                query = query.Where(o => o.VersionKey == agentKey);
            }
            if (diseaseId!= null)
            {
                query = query.Where(o => o.T.ExternalDiseaseId == diseaseId);
            }

            return query.Select(o => o.T);
        }

(由于我没有我无法编译的类,因此您可能需要调整此代码)