我正在尝试在 C# 中合并三个类型为 T 的列表,但是当其中任何一个为 null 时,它会抛出 null 引用异常

I am trying to do union of three lists of type T in C# , but when any of them is null it throws null reference exception

这是我的代码片段:

var joinedList = List1.List.Where(x => x != null)
.Union(List2.List.Where(x=> x!= null).Union(List3.List.Where(x => x!= null))).ToList();
var joinedList = new List<T>();
if (List1.List != null) joinedList = joinedList.Union(List1.List.Where(x=>x!=null);
if (List2.List != null) joinedList = joinedList.Union(List2.List.Where(x=>x!=null);
if (List3.List != null) joinedList = joinedList.Union(List3.List.Where(x=>x!=null);