private static void SelectMany2() { // flatten the year list to return a list of all racers and positions in the championship var racers = Formula1.GetChampionships() .SelectMany(cs => new List <dynamic>() { new { Year = cs.Year, Position = 1, Name = cs.First }, new { Year = cs.Year, Position = 2, Name = cs.Second }, new { Year = cs.Year, Position = 3, Name = cs.Third } }); foreach (var s in racers) { Console.WriteLine(s); } }
private static void CombineRacers() { var q = from r in Formula1.GetChampions() join r2 in Formula1.GetChampionships().GetRacerInfo() on new { FirstName = r.FirstName, LastName = r.LastName } equals new { FirstName = r2.FirstName, LastName = r2.LastName } into yearResults select new { FirstName = r.FirstName, LastName = r.LastName, Wins = r.Wins, Starts = r.Starts, Results = yearResults }; foreach (var item in q) { Console.WriteLine("{0} {1}", item.FirstName, item.LastName); foreach (var item2 in item.Results) { Console.WriteLine("{0} {1}", item2.Year, item2.Position); } } }
static void GroupJoin2() { var racers = Formula1.GetChampionships() .SelectMany(cs => new List <RacerInfo>() { new RacerInfo { Year = cs.Year, Position = 1, FirstName = cs.First.FirstName(), LastName = cs.First.LastName() }, new RacerInfo { Year = cs.Year, Position = 2, FirstName = cs.Second.FirstName(), LastName = cs.Second.LastName() }, new RacerInfo { Year = cs.Year, Position = 3, FirstName = cs.Third.FirstName(), LastName = cs.Third.LastName() } }); var q = (from r in Formula1.GetChampions() join r2 in racers on new { FirstName = r.FirstName, LastName = r.LastName } equals new { FirstName = r2.FirstName, LastName = r2.LastName } into yearResults select new { FirstName = r.FirstName, LastName = r.LastName, Wins = r.Wins, Starts = r.Starts, Results = yearResults }); foreach (var r in q) { Console.WriteLine("{0} {1}", r.FirstName, r.LastName); foreach (var results in r.Results) { Console.WriteLine("{0} {1}", results.Year, results.Position); } } }
// 筛选多个结果 private static void SelectMany2() { var racers = Formula1.GetChampionships() .Where(r => r.Year > 2000) .SelectMany(cs => new List <dynamic>() // dynamic 是定义弱对象 // 自定义对象的属性名 // 然后用 cs 数据赋值; // 将原数据经过封装后, 生成匿名对象返回 { new { Year = cs.Year, Position = 1, Name = cs.First, xx = "hello kitty" } /*, * new { * Year = cs.Year, * Position = 2, * Name = cs.Second * }*/ }); foreach (var s in racers) { Console.WriteLine(s); } }
/* 结果与 下面的 GroupJoin2() 函数相同 */ static void GroupJoinLinq() { var racers = from cs in Formula1.GetChampionships() select new { Year = cs.Year, Numbers = new List <string>() { cs.First, cs.Second, cs.Third } } into temp from r in temp.Numbers select new { Year = temp.Year, FirstName = r.FirstName(), LastName = r.LastName(), Position = temp.Numbers.IndexOf(r) + 1 }; var q = (from r in Formula1.GetChampions() // 只有冠军 join r2 in racers on // 临时匿名对象 new { FirstName = r.FirstName, LastName = r.LastName } equals new { FirstName = r2.FirstName, LastName = r2.LastName } into yearResults select new { FirstName = r.FirstName, LastName = r.LastName, Wins = r.Wins, Starts = r.Starts, Results = yearResults }); foreach (var r in q) { Console.WriteLine("{0} {1}", r.FirstName, r.LastName); foreach (var results in r.Results) { Console.WriteLine("{0} {1}", results.Year, results.Position); } } }
private static void Except() { var racers = Formula1.GetChampionships().SelectMany(cs => new List <RacerInfo>() { new RacerInfo { Year = cs.Year, Position = 1, FirstName = cs.First.FirstName(), LastName = cs.First.LastName() }, new RacerInfo { Year = cs.Year, Position = 2, FirstName = cs.Second.FirstName(), LastName = cs.Second.LastName() }, new RacerInfo { Year = cs.Year, Position = 3, FirstName = cs.Third.FirstName(), LastName = cs.Third.LastName() } }); var nonChampions = racers.Select(r => new { FirstName = r.FirstName, LastName = r.LastName }).Except(Formula1.GetChampions().Select(r => new { FirstName = r.FirstName, LastName = r.LastName })); foreach (var r in nonChampions) { Console.WriteLine("{0} {1}", r.FirstName, r.LastName); } }
/* * 根据名字列表和 年份123名次, 求出每个名字在<哪年>获得<第几名> */ static void GroupJoin2() { var racers = Formula1.GetChampionships() // 年份+123名 /* 把年份 * 1,2,3名 交叉, * * 将 1950 first second third * 形成如下格式: * year pos name * ------------------------------------------ * 1950 1 first(firstname, lastname) * 1950 2 second(firstname, lastname) * 1950 3 third(firstname, lastname) * * 然后用名字列表, 即冠军列表中的名字列表与上表中的 name 作为join 的key */ .SelectMany(cs => new List <RacerInfo>() { // 封装年份 + 123名次 new RacerInfo { Year = cs.Year, Position = 1, FirstName = cs.First.FirstName(), LastName = cs.First.LastName() }, new RacerInfo { Year = cs.Year, Position = 2, FirstName = cs.Second.FirstName(), LastName = cs.Second.LastName() }, new RacerInfo { Year = cs.Year, Position = 3, FirstName = cs.Third.FirstName(), LastName = cs.Third.LastName() } }); var q = (from r in Formula1.GetChampions() // 只有冠军 join r2 in racers on // 临时匿名对象 new { FirstName = r.FirstName, LastName = r.LastName } equals new { FirstName = r2.FirstName, LastName = r2.LastName } into yearResults select new { FirstName = r.FirstName, LastName = r.LastName, Wins = r.Wins, Starts = r.Starts, Results = yearResults }); foreach (var r in q) { Console.WriteLine("{0} {1}", r.FirstName, r.LastName); foreach (var results in r.Results) { Console.WriteLine("{0} {1}", results.Year, results.Position); } } }