2017-12-27 7 views
0

IList<int[]>にある2つのユニークな識別子キーを結合して新しいものに連結したいと考えています。我々はJOIN SQLサーバの2つのテーブルがまったく同じことですが、C#で簡単に行うことができるのだろうか?2つのIListsをジョイントキーで結合する簡単な方法はありますか

コードは次のようになります。

int[] a1 = {1,10}; int[] a2 = {2,20}; int[] a3 = {3,30}; 
int[] b1 = {1,-1}; int[] b2 = {2,-2}; int[] b3 = {3,-3}; 

IList<int[]> List1 = new List<int[]>(); 
IList<int[]> List2 = new List<int[]>(); 

List1.Add(a1);List1.Add(a2);List1.Add(a3); 
List2.Add(b1);List2.Add(b2);List2.Add(b3); 

IList<int[]> ListFinal = new List<int[]>(); 
// I want to the ListFinal be <{1,10,-1},{2,20,-2},{3,30,-3}> 

答えて

4
using System.Linq; 

var ListFinal = List1.Join(List2, 
     (inner) => inner[0], // return key of each item in list1 
     (outer) => outer[0], // return key of each item in list2 to be matched 
     (inner,outer) => new[]{ inner[0],inner[1],outer[1] } 
    ).ToList(); 

また、この関数はそのパラメータ名が述べたように、内側 - 外側が参加します。ペアが見つからないためアイテムが見つからない可能性があります

関連する問題