Linq离开加入了可空的领域

时间:2016-12-14 22:23:52

标签: c# linq

在下面的示例中,我有entity1,其中user_id是可以为null的int,有时它确实为null。 Entity2类似于通过 user_id 将entity1连接到entity3的桥梁,以获取 person_id ,最终获得相应的 fullname

我的目标是编写一个linq查询,其中如果entity1的user_id为null,则不要抛出NullReferenceException,但为fullname属性获取null。

鉴于以下实体,我希望我的查询返回:

  • 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"

实体:

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; }
}

并填写如下:

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" }
};

我的抛出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();

谢谢。

1 个答案:

答案 0 :(得分:7)

使用c#6.0的空传播

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();

注意连接条件和select的简单更改。已将e2.person_id中的e2?.person_id更改为join,将e3.fullname中的e3?.fullname更改为select

但是,如果你不能使用c#6.0,这里有另一种选择

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();