In the following example I have entity1, where the user_id is a nullable int, and sometimes its indeed null. Entity2 is something like a bridge to connect entity1 to entity3, via the user_id, to get the person_id and eventually the corresponding fullname.
My goal would be to write a linq query in which if entity1's user_id is null, then don't throw a NullReferenceException, but get null for the fullname property.
Given the entities below, I would expect my query to return this:
Entities:
class entity1
{
public int? user_id { get; set; }
public string codeOfEntity { get; set; }
}
class entity2
{
public int user_id { get; set; }
public int person_id { get; set; }
}
class entity3
{
public int person_id { get; set; }
public string fullname { get; set; }
}
And filled up like:
var listOfEntity1 = new List<entity1>()
{
new entity1() { user_id = null, codeOfEntity = "21006.040" },
new entity1() { user_id = 10, codeOfEntity = "14006.010" },
new entity1() { user_id = null, codeOfEntity = "21006.020" },
new entity1() { user_id = 1, codeOfEntity = "35716.001"},
new entity1() { user_id = 4, codeOfEntity = "11153.013" }
};
var listOfEntity2 = new List<entity2>()
{
new entity2() { user_id = 1, person_id = 100 },
new entity2() { user_id = 4, person_id = 400 },
new entity2() { user_id = 10, person_id = 1000 }
};
var listOfEntity3 = new List<entity3>()
{
new entity3() { person_id = 100, fullname = "John Smith" },
new entity3() { person_id = 400, fullname = "Dave Doe" },
new entity3() { person_id = 1000, fullname = "Zach White" }
};
My query that throws a NullReferenceException:
var result = (from e1 in listOfEntity1
join e2 in listOfEntity2 on e1.user_id equals e2.user_id into e2Group
from e2 in e2Group.DefaultIfEmpty()
join e3 in listOfEntity3 on e2.person_id equals e3.person_id into e3Group
from e3 in e3Group.DefaultIfEmpty()
select new { e1.codeOfEntity, e3.fullname }).ToList();
Thank you.
Use c#6.0's null propagation
var result = (from e1 in listOfEntity1
join e2 in listOfEntity2 on e1.user_id equals e2.user_id into e2Group
from e2 in e2Group.DefaultIfEmpty()
join e3 in listOfEntity3 on e2?.person_id equals e3.person_id into e3Group
from e3 in e3Group.DefaultIfEmpty()
select new { e1.codeOfEntity, e3?.fullname }).ToList();
Notice the simple change in join condition and select
. Changed e2.person_id
to e2?.person_id
in join
and e3.fullname
to e3?.fullname
in select
.
But, if you are not in a position to use c# 6.0, here is an alternative
var result = (from e1 in listOfEntity1
join e2 in listOfEntity2 on e1.user_id equals e2.user_id into e2Group
from e2 in e2Group.DefaultIfEmpty()
join e3 in listOfEntity3 on (e2==null?-1:e2.person_id) equals e3.person_id into e3Group
from e3 in e3Group.DefaultIfEmpty()
select new { e1.codeOfEntity, fullname=(e3==null?null:e3.fullname) }).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With