标签:des style blog color 使用 io for ar
var racers = from r in Formula1.GetChampions() where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") select r;
var racers = Formula1.GetChampions() .Where(r => r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria")) .Select(r => r);
不能使用 LINQ 查询的一个例子是 Where() 方法的重载。在 Where() 方法的重载中,可以传递第二个参数--索引。索引是筛选器返回的每个结果的计数器。
var racers = Formula1.GetChampions(). Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0);
如果需要根据对象的一个成员进行筛选,而该成员本身是一个系列,就可以使用复合的 from 子句。Racer 类定义了一个属性 Cars,其中 Cars 是一个字符串数组。要筛选驾驶法拉利的所有冠军,可以使用如下所示的 LINQ 查询:
var ferrariDrivers = from r in Formula1.GetChampions() from c in r.Cars where c == "Ferrari" orderby r.LastName select r.FirstName + " " + r.LastName;
var ferrariDrivers = Formula1.GetChampions() .SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c }) .Where(r => r.Car == "Ferrari") .OrderBy(r => r.Racer.LastName) .Select(r => r.Racer.FirstName + " " + r.Racer.LastName);
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);
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 countries = 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() });
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 };
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 };
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);
标签:des style blog color 使用 io for ar
原文地址:http://www.cnblogs.com/otfngo/p/3921587.html