gpt4 book ai didi

c# - Linq 左连接可空字段

转载 作者:行者123 更新时间:2023-11-30 14:07:25 25 4
gpt4 key购买 nike

在下面的示例中,我有 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();

谢谢。

最佳答案

使用c#6.0的null传播

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 的简单变化。将 join 中的 e2.person_id 更改为 e2?.person_id 并将 e3.fullname 更改为 e3?。 选择中的全名

但是,如果您不能使用 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();

关于c# - Linq 左连接可空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41153284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com