.Net Entity 两个可选参数

.Net Entity two optional parameters

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

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. 你的 ifs 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);
        }

(因为我没有 类 我无法编译这个,你可能需要调整这个代码)