Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq left join on nullable field

Tags:

c#

linq

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:

  • codeOfEntity = "21006.040", fullname = null
  • codeOfEntity = "14006.010", fullname = "John Smith"
  • codeOfEntity = "21006.020", fullname = null
  • codeOfEntity = "35716.001", fullname = "Dave Doe"
  • codeOfEntity = "11153.013", fullname = "Zach White"

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.

like image 402
hungariandude Avatar asked Mar 11 '23 10:03

hungariandude


1 Answers

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();
like image 134
Sateesh Pagolu Avatar answered Mar 20 '23 03:03

Sateesh Pagolu