// 使用 Linq 函数实现 Innerjoin // 结果 与 InnerJoin() 相同 static void InnerJoinMethod() { // 表内交叉 var racers = Formula1.GetChampions(). SelectMany(r => r.Years, (r, y) => new { reacer = r, year = y }); // 表内交叉 var teams = Formula1.GetContructorChampions(). SelectMany(t => t.Years, (t, y) => new { team = t, year = y }); // 两个结果再交叉 var racersAndTeams = racers.Join(teams, r => r.year, t => t.year, (r, t) => new { Year = r.year, Champion = r.reacer.FirstName, Constructor = t.team.Name }). OrderBy(y => y.Year). Take(10); foreach (var item in racersAndTeams) { Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Champion, item.Constructor); } }
static void GroupJoin() { // var q = //from c in categories //join p in products on c equals p.Category into ps //select new { Category = c, Products = ps }; var racers = from r in Formula1.GetChampions() from y in r.Years select new { Year = y, Name = r.FirstName + " " + r.LastName }; var teams = from t in Formula1.GetContructorChampions() from y in t.Years select new { Year = y, Name = t.Name }; var racersAndTeams = from r in racers join t in teams on r.Year equals t.Year into ts select new { Year = r.Year, Racer = r.Name, Constructor = ts }; foreach (var r in racersAndTeams) { Console.WriteLine("{0} {1}", r.Year, r.Racer); foreach (var t in r.Constructor) { Console.WriteLine("\t{0}", t.Name); } } }
// 内链接, 只包含 Year 相等的两边数据 static void InnerJoin() { // 表内交叉 var racers = from r in Formula1.GetChampions() from y in r.Years select new { Year = y, Name = r.FirstName + " " + r.LastName }; // 表内交叉 var teams = from t in Formula1.GetContructorChampions() from y in t.Years select new { Year = y, Name = t.Name }; // 两表交叉 var racersAndTeams = (from r in racers join t in teams on r.Year equals t.Year orderby t.Year // t 表 teams, r 代表 racers select new { Year = r.Year, Champion = r.Name, Constructor = t.Name }).Take(10); // 只显示前 10 条数据 Console.WriteLine("Year World Champion\t Constructor Title"); foreach (var item in racersAndTeams) { Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Champion, item.Constructor); } }
static void Join() { var racers = from r in Formula1.GetChampions() from y in r.Years where y > 2003 select new { Year = y, Name = r.FirstName + " " + r.LastName }; var teams = from t in Formula1.GetContructorChampions() from y in t.Years where y > 2003 select new { Year = y, Name = t.Name }; var racersAndTeams = from r in racers join t in teams on r.Year equals t.Year select new { Year = r.Year, Racer = r.Name, Team = t.Name }; Console.WriteLine("Year Champion " + "Constructor Title"); foreach (var item in racersAndTeams) { Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Racer, item.Team); } }
static void LeftOuterJoin() { var racers = from r in Formula1.GetChampions() from y in r.Years select new { Year = y, Name = r.FirstName + " " + r.LastName }; var teams = from t in Formula1.GetContructorChampions() from y in t.Years select new { Year = y, Name = t.Name }; var racersAndTeams = (from r in racers join t in teams on r.Year equals t.Year into rt from t in rt.DefaultIfEmpty() orderby r.Year select new { Year = r.Year, Champion = r.Name, Constructor = t == null ? "no constructor championship" : t.Name }).Take(10); Console.WriteLine("Year Champion\t\t Constructor Title"); foreach (var item in racersAndTeams) { Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Champion, item.Constructor); } }
private static void Join() { //var racers = from r in Formula1.GetChampions() // from y in r.Years // where y > 2003 // select new { Year = y, Name = r.FirstName + " " + r.LastName }; //var teams = from t in Formula1.GetContructorChampions() // from y in t.Years // where y > 2003 // select new { Year = y, Name = t.Name }; //var racersAndTeams = from r in racers // join t in teams on r.Year equals t.Year // select new { Year = r.Year, Racer = r.Name, Team = t.Name }; int year = 2003; var racersAndTeams = from r in from r1 in Formula1.GetChampions() from yr in r1.Years where yr > year select new { Year = yr, Name = r1.FirstName + " " + r1.LastName } join t in from t1 in Formula1.GetContructorChampions() from yt in t1.Years where yt > year select new { Year = yt, Name = t1.Name } on r.Year equals t.Year select new { Year = r.Year, Racer = r.Name, Team = t.Name }; Console.WriteLine("Year Champion Constructor Title"); foreach (var item in racersAndTeams) { Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Racer, item.Team); } }