Join the difference and GroupJoin
List<Atable> ainfo = new List<Atable> { new Atable{ AId=1, AName="A1", AAge="11" }, new Atable{ AId=2, AName="A2", AAge="12"}, new Atable{ AId=3, AName="A3",AAge="13"} }; List<Btable> binfo = new List<Btable>() { new Btable{ BId=1, BName="B1", BAge="81" }, new Btable{ BId=3, BName="B3", BAge="83"}, new Btable{ BId=5, BName="B5",BAge="85"} };
Join
Official Interpretation: based on matching key elements of two sequences are related. The default equality comparator to compare keys.
This is very similar to the database INNER JOIN, is to use a key (TKey) associating two sets, and these two sets of elements are selected as the output result.
var info = ainfo.Join (binfo, // need to connect to a data source A => a.AId, B => b.BId, (A, B) => new new {a.AName, b.BName}); / / get your own set of defined types. the foreach ( var Item in info) { Console.WriteLine (item.AName + item.BName); }
GroupJoin
Official Interpretation: groups the results based on the key elements of two sequences equal associating. The default equality comparator to compare keys.
This is very similar to the LEFT OUTER JOIN database. Join is the difference between: the input parameters GroupJoin resultSelector TInner individual elements from a set of programming IEnumerable <TInner> element, other remain unchanged. Join usage and almost, it is also based on TOuter.TKey and TInner.TKey connection.
var info = ainfo.GroupJoin (binfo, // need to connect to the data source aTable => Atable.AId, // by AId specified data source BTABLE => Btable.BId, // by BId specified data source (a, b) = > new new { a.AName, binfos = B } // create the same result data ) .ToList (); the foreach ( var Item in info) { the foreach ( var B in item.binfos) { Console.WriteLine(item.AName + "\t" + b.BName); } }