gpt4 book ai didi

c# - 在 LINQ 中加入两个集合

转载 作者:太空宇宙 更新时间:2023-11-03 22:21:36 26 4
gpt4 key购买 nike

    var setsA = new List<SetA> {
new SetA { SsnA = "3450734507", name = "setA"},
new SetA { SsnA = "6833467788", name = "setA"},
new SetA { SsnA = "5452347787", name = "setA"},
new SetA { SsnA = "9345345345", name = "setA"},
};

var setsB = new List<SetB> {
new SetB { SsnB = "5452347787" ,name = "setB"},
new SetB { SsnB = "9345345345", name = "setB"},
};

当我使用这个 linq 时:

var Set =
from seta in setsA
join setb in setsB
on seta.SsnA
equals setb.SsnB
select new {
SSN = seta.SsnA,
NAME = setb.name
};

我得到这个值:

{ SSN = "5452347787", NAME = "setB" }
{ SSN = "9345345345", NAME = "setB" }

但我想要将这两者结合起来的 SET,结果将是:

{ SSN = "3450734507", NAME = "setA" }
{ SSN = "6833467788", NAME = "setA" }
{ SSN = "5452347787", NAME = "setB" }
{ SSN = "9345345345", NAME = "setB" }

这将是一个结果集,如果在 SetA 和 SetB 中找到 SSN,它将告诉我名称 NAME 属性是从哪个集合中获取的,它将具有属性 NAME = "setB"

有人可以帮我吗?

最佳答案

您似乎想要一个外部联接 - 这是使用 GroupJoin 完成的:

var set = setsA.GroupJoin(
setsB,
sa => sa.SsnA,
sb => sb.SsnB,
(a, bs) => new { SSN = a.SsnA, NAME = bs.Any() ? "setB" : "setA" });

关于c# - 在 LINQ 中加入两个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2867567/

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