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
object
to 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