希望将两种方法与c#相结合



HI我有下面两个方法,希望将两者结合起来,其中一个方法不需要任何连接,另一个方法需要这些连接。我如何将这两个方法结合起来

internal static IQueryable<ConstructionSet> CalculateSelectedConstructionSetsForOSM(Guid? ashraeClimateZoneId,
                  Guid? energyCodeId,
                  Guid? massingTypeId,
                  APIDbContext dbContext)
{
if (dbContext is null)
{
throw new ArgumentNullException(nameof(dbContext));
}
return dbContext.ConstructionSets.Include(p => p.AshraeClimateZone)
.Include(p => p.MassingType)
.Include(p => p.SourceOfData)
.Include(p => p.ExteriorWall)
.Include(p => p.ExteriorFloor)
.Include(p => p.InteriorFloor)
.Include(p => p.InteriorWall)
.Include(p => p.SlabOnGrade)
.Include(p => p.BelowGradeWall)
.Include(p => p.Glazing)
.Include(p => p.Roof)
.Where(p => p.AshraeClimateZoneId == ashraeClimateZoneId
&& p.SourceOfDataId == energyCodeId
&& p.MassingTypeId == massingTypeId
&& p.Revision != null
&& p.IsApproved == true)
.AsEnumerable()
.OrderByDescending(i => i.Revision)
.GroupBy(i => i.InitialRevisionId)
.Select(g => g.First()).AsQueryable();
}

下面是的另一种方法

internal static IQueryable<ConstructionSet> CalculateSelectedConstructionSetsForProject(Guid? ashraeClimateZoneId,
                      Guid? energyCodeId,
                      Guid? massingTypeId,
                      APIDbContext dbContext)
{
if (dbContext is null)
{
throw new ArgumentNullException(nameof(dbContext));
}
return dbContext.ConstructionSets.Include(p => p.AshraeClimateZone)
.Include(p => p.MassingType)
.Include(p => p.SourceOfData)
.Where(p => p.AshraeClimateZoneId == ashraeClimateZoneId
&& p.SourceOfDataId == energyCodeId
&& p.MassingTypeId == massingTypeId
&& p.Revision != null
&& p.IsApproved == true)
.AsEnumerable()
.OrderByDescending(i => i.Revision)
.GroupBy(i => i.InitialRevisionId)
.Select(g => g.First()).AsQueryable();
}

有谁能告诉我如何将这些方法结合起来吗?非常感谢。

假设;将它们"组合";你的意思是";重构它们,使得单个方法可以处理这两种情况";,你可以这样做:

internal static IQueryable<ConstructionSet> CalculateSelectedConstructionSets(Guid? ashraeClimateZoneId,
              Guid? energyCodeId,
              Guid? massingTypeId,
              APIDbContext dbContext,
              bool includeOsmNavigationProperties)
{
if (dbContext is null)
{
throw new ArgumentNullException(nameof(dbContext));
}
var sets = dbContext.ConstructionSets.Include(p => p.AshraeClimateZone)
.Include(p => p.MassingType)
.Include(p => p.SourceOfData);
if(includeOsmNavigationProperties)
{
sets = sets.Include(p => p.ExteriorWall)
.Include(p => p.ExteriorFloor)
.Include(p => p.InteriorFloor)
.Include(p => p.InteriorWall)
.Include(p => p.SlabOnGrade)
.Include(p => p.BelowGradeWall)
.Include(p => p.Glazing)
.Include(p => p.Roof);
}
return sets.Where(p => p.AshraeClimateZoneId == ashraeClimateZoneId
&& p.SourceOfDataId == energyCodeId
&& p.MassingTypeId == massingTypeId
&& p.Revision != null
&& p.IsApproved == true)
.AsEnumerable()
.OrderByDescending(i => i.Revision)
.GroupBy(i => i.InitialRevisionId)
.Select(g => g.First()).AsQueryable();
}

附带说明一下,您应该避免将其转换为.AsEnumerable()然后再转换回.AsQueryable()的模式。将结果设为IQueryable会使对返回集合的后续操作慢得多(表达式需要在运行时编译(,并且它隐藏了不会在数据库层执行后续操作的事实。有些人可能添加了Where()子句,却没有意识到它们仍然会导致整个数据集从数据库中加载出来。

相关内容

最新更新