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); } } }
static void ZipOperation() { var racerNames = from r in Formula1.GetChampions() where r.Country == "Italy" orderby r.Wins descending select new { Name = r.FirstName + " " + r.LastName }; var racerNamesAndStarts = from r in Formula1.GetChampions() where r.Country == "Italy" orderby r.Wins descending select new { LastName = r.LastName, Starts = r.Starts }; var racers = racerNames.Zip(racerNamesAndStarts, (first, second) => first.Name + ", starts: " + second.Starts); foreach (var r in racers) { Console.WriteLine(r); } }
static void GroupingWithNestedObjects() { var countries = from r in Formula1.GetChampions() group r by r.Country into g orderby g.Count() descending, g.Key where g.Count() >= 2 select new { Country = g.Key, Count = g.Count(), Racers = from r1 in g orderby r1.LastName select r1.FirstName + " " + r1.LastName }; foreach (var item in countries) { Console.WriteLine("{0, -10} {1}", item.Country, item.Count); foreach (var name in item.Racers) { Console.Write("{0}; ", name); } Console.WriteLine(); } }
// 使用 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); } }
private static void Conversion() { //// query executed immediately //List<Racer> racers = (from r in Formula1.GetChampions() // where r.Starts > 150 // orderby r.Starts descending // select r).ToList(); //foreach (var racer in racers) //{ // Console.WriteLine("{0} {0:S}", racer); //} ILookup <string, Racer> racers = (from r in Formula1.GetChampions() from c in r.Cars select new { Car = c, Racer = r }).ToLookup(cr => cr.Car, cr => cr.Racer); if (racers.Contains("Williams")) { foreach (var williamsRacer in racers["Williams"]) { Console.WriteLine(williamsRacer); } } }
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); } } }
private static void SimpleFilteringWithIndex() { var racers = Formula1.GetChampions().Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0); foreach (var r in racers) { Console.WriteLine("{0:A}", r); } }
private static void Sorting() { var racers = (from r in Formula1.GetChampions() orderby r.Country, r.LastName, r.FirstName select r).Take(10); foreach (var racer in racers) { Console.WriteLine("{0}: {1}, {2}", racer.Country, racer.LastName, racer.FirstName); } }
static void Filtering() { var racers = from r in Formula1.GetChampions() where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") select r; foreach (var r in racers) { Console.WriteLine("{0:A}", r); } }
static void LINQQuery() { var query = from r in Formula1.GetChampions() where r.Country == "Brazil" orderby r.Wins descending select r; foreach (var r in query) { Console.WriteLine("{0:A}", r); } }
private static void LinqQuery() { List <Racer> champions = new List <Racer>(Formula1.GetChampions()); var query = from r in Formula1.GetChampions() where r.Country == "Brazil" orderby r.Wins descending select r; foreach (Racer r in query) { Console.WriteLine("{0:A}", r); } }
private static void LambdaExpressions() { List <Racer> champions = new List <Racer>(Formula1.GetChampions()); IEnumerable <Racer> brazilChampions = champions. Where(r => r.Country == "Brazil"). OrderByDescending(r => r.Wins). Select(r => r); foreach (Racer r in brazilChampions) { Console.WriteLine("{0:A}", r); } }
// 多个 from 子句实现交叉表查询 // 如果筛选结果里面有集合, 就可以使用 from 进一步筛选 static void CompoundFrom() { var ferrariDrivers = from r in Formula1.GetChampions() from c in r.Cars // 相当于交叉表一样, 如果 Cars 有多个 Ferrari, 结果就会重复出现几次 where c == "Ferrari" || c == "Lotus" // orderby r.LastName select r.FirstName + " " + r.LastName + " - " + c; foreach (var racer in ferrariDrivers) { Console.WriteLine(racer); } }
static void CompoundFrom() { var ferrariDrivers = from r in Formula1.GetChampions() from c in r.Cars where c == "Ferrari" orderby r.LastName select r.FirstName + " " + r.LastName; foreach (var racer in ferrariDrivers) { Console.WriteLine(racer); } }
static void Untyped() { var list = new System.Collections.ArrayList(Formula1.GetChampions() as System.Collections.ICollection); var query = from r in list.Cast <Racer>() where r.Country == "USA" orderby r.Wins descending select r; foreach (var racer in query) { Console.WriteLine("{0:A}", racer); } }
static void SetOperations() { Func <string, IEnumerable <Racer> > racersByCar = car => from r in Formula1.GetChampions() from c in r.Cars where c == car orderby r.LastName select r; Console.WriteLine("World champion with Ferrari and McLaren"); foreach (var racer in racersByCar("Ferrari").Intersect(racersByCar("McLaren"))) { Console.WriteLine(racer); } }
private static void SimpleFiltering() { var racers = from r in Formula1.GetChampions() where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") select r; var racers2 = Formula1.GetChampions(). Where(r => r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria")). Select(r => r); foreach (var r in racers2) { Console.WriteLine("{0:A}", r); } }
// Cast 转换对象类型 static void Untyped() { var list = new System.Collections.ArrayList(Formula1.GetChampions() as System.Collections.ICollection); // cast 将只能以 [index] 访问的集合转换成 .propertyName 的形式, 这样才可以用 foreach 遍历 var query = from r in list.Cast <Racer>() where r.Country == "USA" orderby r.Wins descending select r; foreach (var racer in query) { Console.WriteLine("{0:A}", racer); } }
private static void QuantifiersCount() { var racers = from r in Formula1.GetChampions() where r.Years.Count() >= 3 orderby r.Years.Count() descending select new { Name = r.FirstName + " " + r.LastName, TimesChampion = r.Years.Count() }; foreach (var r in racers) { Console.WriteLine("{0} {1}", r.Name, r.TimesChampion); } }
static void Aggregate() { var query = from r in Formula1.GetChampions() where r.Years.Count() > 3 orderby r.Years.Count() descending select new { Name = r.FirstName + " " + r.LastName, TimesChampion = r.Years.Count() }; foreach (var r in query) { Console.WriteLine("{0} {1}", r.Name, r.TimesChampion); } }
static void Grouping() { var countries = from r in Formula1.GetChampions() group r by r.Country into g orderby g.Count() descending, g.Key where g.Count() >= 2 select new { Country = g.Key, Count = g.Count() }; foreach (var item in countries) { Console.WriteLine("{0, -10} {1}", item.Country, item.Count); } }
private static void Ordering() { //var racers = (from r in Formula1.GetChampions() // orderby r.Country, r.LastName, r.FirstName // select r).Take(10); var racers = Formula1.GetChampions(). OrderBy(r => r.Country). ThenBy(r => r.LastName). ThenBy(r => r.FirstName). Take(10); foreach (var racer in racers) { Console.WriteLine("{0:C}: {0:L}, {0:F}", racer); } }
/* 结果与 下面的 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 GroupingAndAggregation() { var countries = from c in from r in Formula1.GetChampions() group r by r.Country into g select new { Country = g.Key, Wins = (from x in g select x.Wins).Sum() } orderby c.Wins descending select c; foreach (var item in countries) { Console.WriteLine(item); } }
static void Aggregate2() { var countries = (from c in from r in Formula1.GetChampions() group r by r.Country into c select new { Country = c.Key, Wins = (from r1 in c select r1.Wins).Sum() } orderby c.Wins descending, c.Country select c).Take(5); foreach (var country in countries) { Console.WriteLine("{0} {1}", country.Country, country.Wins); } }
private static void Grouping() { var countries = from r in Formula1.GetChampions() group r by r.Country into g orderby g.Count() descending, g.Key where g.Count() >= 2 select new { Country = g.Key, Count = g.Count() }; //var query = Formula1.GetChampions(). // GroupBy(r => r.Country). // OrderByDescending(g => g.Count()). // ThenBy(g => g.Key). // Where(g => g.Count() >= 2).Select(g => new { Country = g.Key, Count = g.Count() }); foreach (var item in countries) { Console.WriteLine("{0,-10} {1}", item.Country, item.Count); } }
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); } } }
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); } }
private static void TraditionalQuery() { List <Racer> champions = new List <Racer>(Formula1.GetChampions()); List <Racer> brazilChampions = champions.FindAll( delegate(Racer r) { return(r.Country == "Brazil"); }); brazilChampions.Sort( delegate(Racer r1, Racer r2) { return(r2.Wins.CompareTo(r1.Wins)); }); foreach (Racer r in brazilChampions) { Console.WriteLine("{0:A}", r); } }
private static void Partitioning() { int pageSize = 5; int numberPages = (int)Math.Ceiling(Formula1.GetChampions().Count() / (double)pageSize); for (int page = 0; page < numberPages; page++) { Console.WriteLine("Page {0}", page); var racers = (from r in Formula1.GetChampions() orderby r.LastName select r.FirstName + " " + r.LastName).Skip(page * pageSize).Take(pageSize); foreach (var name in racers) { Console.WriteLine(name); } Console.WriteLine(); } }