不知道如何優化這個,而是應該滿足您的需求 - 假設我理解你想要的東西:
var enumerable1 = new[]
{
new {Id = "A", Value = 1.0},
new {Id = "B", Value = 2.0},
new {Id = "C", Value = 3.0},
new {Id = "D", Value = 4.0},
new {Id = "E", Value = 5.0},
};
var enumerable2 = new[]
{
new {Id = "A", Value = 6.0},
new {Id = "NOT PRESENT", Value = 542.23},
new {Id = "C", Value = 7.0},
new {Id = "D", Value = 8.0},
new {Id = "E", Value = 9.0},
};
var result = enumerable1.Join(enumerable2, arg => arg.Id, arg => arg.Id,
(first, second) => new {Id = first.Id, Value1 = first.Value, Value2 = second.Value});
foreach (var item in result)
Console.WriteLine("{0}: {1} - {2}", item.Id, item.Value1, item.Value2);
Console.ReadLine();
輸出結果將是一個類似於:
A: 1 - 6
C: 3 - 7
D: 4 - 8
E: 5 - 9
不要真正明白爲什麼你會希望返回空值,除非你絕對需要(除此之外,double不是空的,所以它必須是結果的組合條目,而不是null)。
這可能是使用'Zip'方法:http://msdn.microsoft.com/en-ca/library/vstudio/dd267698%28v=vs.100%29.aspx – Matthew 2014-09-03 13:30:52
@Matthew'Zip '方法不能用來做到這一點。 – Stijn 2017-09-04 14:19:49