0

I have an api that has the following model keep in my that this's just an exemple to get the whole idea of my problem

namespace Models{
    public class A
    {
        public int Id{get;set;}
        public List<B> Bs{get;set;}
        .
        .
    }


    public class B
    {
        public int Id{get;set;}
        public List<C> Cs{get;set;}
        .
        .
    }

    public class C
    {
        public int Id{get;set;}
        .
        .
    }
}
namespace Api{
    public class Aprime
    {
        public int Id{get;set;}
        public List<BPrime> BPrimes{get;set;}
        public int Calculation{get;set;}
        .
        .
    }

    public class Bprime
    {
        public int Id{get;set;}
        public List<CPrime> CPrimes{get;set;}
        .
        .
    }

    public class CPrime
    {
        public int Id{get;set;}
        .
        .
    }
}

I have a method that do some work and give me the result and this method work percetly in this case

public ICollection<Api.Aprime> Foo(){
    var result = query.TableA.something().Select(dataFromA=> 
        new Api.Aprime
        {
            Id = dataFromA.Id,
            BPrimes = dataFromA.Bs.Where(dataFromB=> Condition).Select(dataFromB
                new Api.BPrime
                {
                    Id=dataFromB.Id,
                    CPrimes=dataFromB.Cs.Where(dataFromC=> Condition).Select(
                        new Api.CPrime()
                        {
                            Id = dataFromC.Id,
                        })
                }
            )
        }
    );
}

Everthing that exist before this work perfectly. My problem is that, If have a method that create for exemple Bprime objectto simplify the reading of the query and code. Thus doing so generate a NullReferenceException however it work in one case when the transformation is made in one lvl and there's no query that need a navigation inside it like the following

public ICollection<Api.Aprime> FooWithTransformation(){
    var result = query.TableA.something().Select(dataFromA=> 
        new Api.Aprime
        {
            Id = dataFromA.Id,
            BPrimes = dataFromA.Bs.Where(dataFromB=> Condition).Select(dataFromB
                new Api.BPrime
                {
                    Id=dataFromB.Id,
                    CPrimes=dataFromB.Cs.Where(dataFromC=> Condition).Select(dataFromC=>dataFromC.ToCprimes()) //lvl 1 
                }
            )
        }
    );
}

but I want to do is this kind of writing but it wont work in this following case because there's a navigation

public ICollection<Api.Aprime> FooWithNullExceptionInTransformation(){
    var result = query.TableA.something().Select(dataFromA=> 
        new Api.Aprime
        {
            Id = dataFromA.Id,
            // Won't work because the C collection in this case is null and I got an exception
            Bs = dataFromA.Bs.Where(dataFromB=> Condition).Select(dataFromB=>dataFromB.ToBprimes())
        }
    );
}

I use ef core with an azure database

Hicham Bouchilkhi
  • 682
  • 10
  • 29
  • Without something like [this](https://stackoverflow.com/a/66386142/10646316), better to stay with working solution. DRY needs some efforts when building LINQ query. – Svyatoslav Danyliv Dec 01 '22 at 14:36

0 Answers0