gpt4 book ai didi

C# 使用 Linq 加入 3 个列表

转载 作者:行者123 更新时间:2023-12-03 22:58:27 25 4
gpt4 key购买 nike

很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。为了帮助澄清这个问题,以便它可以重新打开, visit the help center




8年前关闭。




我想加入 3 个具有以下格式的列表:

List1:
CountryID | CountryData | regionUID
===================================
12 | Has good gras | 4567
12 | nice weather | 6789
16 | stormy weather | 1234

List2:
CountryID | CountryName
=======================
12 | green hill
16 | stormy mountain

List3:
regionUID | regionName
=======================
4567 | above 1000feet
6789 | on the ground
1234 | on the hill

输出应如下所示:
CountryName      | CountryData      | regionName
==============================================
green hill | Has good gras | above 1000feet
green hill | nice weather | on the ground
stormy mountain | stormy weather | on the hill

地区是独一无二的,国家可以适合多条线

最佳答案

你想要这样的东西:

var list1 = new []{
new { CountryID = 12, CountryData = "Has good gras", regionUID = 4567 },
new { CountryID = 12, CountryData = "nice weather", regionUID = 6789 },
new { CountryID = 16, CountryData = "stormy weather", regionUID = 1234 }};

var list2 = new []{
new { CountryID = 12, CountryName = "green hill"},
new { CountryID = 16, CountryName = "stormy mountain"}
};

var list3 = new []{
new {regionUID = 4567, regionName = "above 1000feet"},
new {regionUID = 6789, regionName = "on the ground"},
new {regionUID = 1234, regionName = "on the hill"}
};

var result = from m1 in list1
join m2 in list2 on m1.CountryID equals m2.CountryID
select new { m2.CountryName, m1.CountryData, m1.regionUID } into intermediate
join m3 in list3 on intermediate.regionUID equals m3.regionUID
select new { intermediate.CountryName, intermediate.CountryData, m3.regionName};

RW

关于C# 使用 Linq 加入 3 个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677871/

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