如何使用Linq对包含集合的集合进行非规范化

dub*_*ubs 5 c# linq

我正在尝试转换以下集合:

资源

"a", "b", {1,2,3}
"d", "f", {1,2,2}
"y", "z", {}
Run Code Online (Sandbox Code Playgroud)

目的地

"a", "b", 1
"a", "b", 2
"a", "b", 3
"d", "f", 1
"d", "f", 2
"d", "f", 2
"y", "z", null
Run Code Online (Sandbox Code Playgroud)

我已经对其进行了研究,并认为答案位于SelectMany()方法的某个位置,但我似乎可以确定答案。

问题类似于:如何使用LINQ在集合中选择一个集合?这会规范化集合,但未显示如何包括相关列(在我的示例中为列1和2)。

区别在于,我需要包括前两列,并且还返回在集合中没有任何条目的行。

Den*_*s_E 2

使用元组列表作为示例:

var source = new List<Tuple<string, string, int[]>> {
    Tuple.Create("a", "b", new int[] {1,2,3}),
    Tuple.Create("d", "f", new int[] {1,2,2}),
    Tuple.Create("y", "z", new int[0])
};


var destination =
from t in source
from i in t.Item3.Select(x => (int?)x).DefaultIfEmpty()
select Tuple.Create(t.Item1, t.Item2, i);
Run Code Online (Sandbox Code Playgroud)