尝试在 if 语句中添加不同的类型

trying to add different types inside if statement

我有一个下面的方法,我循环遍历 id 列表并根据 id 从 db 获取数据,然后创建 material 然后添加到 material 列表

public Construction AddToOsm(Model model, APIDbContext dbContext)
{   
    var construction = new Construction(model);
    var surfaceType = dbContext.IntendedSurfaceTypes.SingleOrDefault(s => s.Id == this.SurfaceTypeId);
    construction.setName(surfaceType?.Name);
    using var materials = new MaterialVector();

    var fenestrationMaterialById = new Dictionary<Guid, FenestrationMaterial>();
    var opaqueMaterialById = new Dictionary<Guid, StandardOpaqueMaterial>();
    foreach (var materialId in this.LayerIds.Where(i => i != default))
    {
        var opaqueMaterial = dbContext.OpaqueMaterials.SingleOrDefault(o => o.Id == materialId);
        if (opaqueMaterial != default)
        {
            materials.Add(opaqueMaterialById.GetOrCreate(opaqueMaterial.Id, () => opaqueMaterial.AddToOsm(model)));
        }
        else
        {
            var glazingMaterial = dbContext.GlazingMaterials.SingleOrDefault(o => o.Id == materialId);
            if (glazingMaterial != default)
            {
                materials.Add(fenestrationMaterialById.GetOrCreate(glazingMaterial.Id, () => glazingMaterial.AddToOsm(model)));
            }
            else
            {
                var glazingSimpleMaterial = dbContext.SimpleGlazingMaterials.SingleOrDefault(s => s.Id == materialId);
                if(glazingSimpleMaterial != default)
                {
                    materials.Add(fenestrationMaterialById.GetOrCreate(glazingSimpleMaterial.Id, () => glazingSimpleMaterial.AddToOsm(model)));
                }
                else
                {
                    var gasGlazingMaterials = dbContext.GasGlazingMaterials.SingleOrDefault(a => a.Id == materialId);
                    if(gasGlazingMaterials != default)
                    {
                        materials.Add(fenestrationMaterialById.GetOrCreate(gasGlazingMaterials.Id, () => gasGlazingMaterials.AddToOsm(model)));
                    }
                }
            }
        }
    }
    construction.setLayers(materials);
    return construction;
}    

我正在寻找一种方法来避免这么多 if-else 语句,主要是重构它,但找不到方法。任何人都可以就如何实现相同的目标提出任何想法。

提前致谢。

更新:示例实体结构

public class GasGlazingMaterial : ISourceOfData, IIdentity<Guid>
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    [ForeignKey("SourceOfData")]
    public Guid? SourceOfDataId { get; set; }
    public virtual CodeStandardGuideline SourceOfData { get; set; }
    ......
    .....
 }

一个简单的解决方法是在每个 materials.add 之后“继续”。这意味着您不需要将其余部分嵌入 else