Linq规范查询操纵符
言语集查询 Ganmge hteg.ratedQuw,LINQ,集 成了C#编程言语中的查询语法 ,可 以用雷同的语法接见差别的数据源。 LINQ提 供了差别数据源的笼统层 ,所 以能够运用雷同的语法。
1挑选操纵符
挑选操纵符定义了返回元素的前提。
挑选操纵符 | 形貌 |
---|---|
where | 运用谓词,返回布尔值 |
OfType< TResult> | 依据范例挑选元素 |
运用举例:
where用法:
var racers = from r in Formulal.Racers where r.Wins > 15 && r.Country == "Brazil" select r;
OfType< T>用法:
object[] data = {"one", 2, "three", 4, 5, "six"}; var rtnData = data.OfType<string>(); //返回范例为string的数据元素鸠合
2投射操纵符
把对象转化为另一个范例的新对象
投射操纵符 | 形貌 |
---|---|
select | |
selectMany |
用法举例:
select用法
var racers = from r in Formulal.Racers where r.Wins > 15 && r.Country == "Brazil" select new { Name = string.Format("{0} {1}", r.FirstName, r.LastName), Country = r.Country, Wins = r.Wins }; //输出为含有Name,Country,Wins总计3个属性的对象鸠合
selectMany用法
var ferrariDrivers = Formulal.Racers. 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);//挑选出驾驶法拉利的选手鸠合
3排序操纵符
转变返回元素的递次
排序操纵符 | 形貌 |
---|---|
OrderBy | 升序排序 |
OrderByDescending | 降序排序 |
ThenBy | 第一次排序效果相似,用第二次升序排序 |
ThenByDescending | 第一次排序效果相似,用第二次降序排序 |
Reverse | 反转鸠合中元素的递次 |
举例:
var racers = from r in Formulal.Racers where r.Country == "Brazil" orderby r.Wins descending select new { Name = string.Format("{0} {1}", r.FirstName, r.LastName), Country = r.Country, Wins = r.Wins };//国度为巴西,根据胜场次数降序分列
4衔接运算符
用于兼并不直接相干的鸠合
衔接运算符 | 形貌 |
---|---|
Join | 能够依据键选择器函数衔接衔接2个鸠合 |
GroupJoin | 衔接两个鸠合,组合其效果 |
//Join衔接 //猎取冠军年份在2003年后的选手 var racer5 = from r in Formulal.Racers from y in r.Years where y > 2003 select new { Year = y, Name = string.Format("{0} {1}", r.FirstName, r.LastName) }; //猎取冠军年份在2003年后的团队 var team1 = from r in Formulal.ChampionTeams from y in r.Years where y > 2003 select new { Year = y, Name = string.Format("{0}", r.Name) }; //衔接 var racerAndTeams = from r in racer5 join t in team1 on r.Year equals t.Year select new { Yearj= r.Year, Racerj = r.Name, Teamj = t.Name };
效果:
Yearj | Racerj | Teamj |
---|---|---|
Yearj=2004 | Racerj=Michael Schumacher | Teamj =Ferrari |
Yearj=2005 | Racerj=Fernando Alonso | Teamj =Renault |
Yearj=2006 | Racerj=Fernando Alonso | Teamj =Renault |
Yearj=2007 | Racerj=Kimi Räikkönen | Teamj =Ferrari |
Yearj=2008 | Racerj=Lewis Hamilton | Teamj =Ferrari |
Yearj=2014 | Racerj=Lewis Hamilton | Teamj =Mercedes |
Yearj=2015 | Racerj=Lewis Hamilton | Teamj =Mercedes |
Yearj=2009 | Racerj=Jenson Button | Teamj =Brawn GP |
Yearj=2010 | Racerj=Sebastian Vettel | Teamj =Red Bull Racing |
Yearj=2011 | Racerj=Sebastian Vettel | Teamj =Red Bull Racing |
Yearj=2012 | Racerj=Sebastian Vettel | Teamj =Red Bull Racing |
Yearj=2013 | Racerj=Sebastian Vettel | Teamj =Red Bull Racing |
5组合运算符
把数据放在组中
组合运算符 | 形貌 |
---|---|
GroupBy | 组合有大众键的元素 |
ToLookUp | 经由过程建立一个一对多的字典,来组合元素。 |
GroupBy用法举例:
var countries = from r in Formulal.Racers 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() };//猎取冠军次数大于2的国度
效果:
Country | Count |
---|---|
UK | 10 |
Brazil | 3 |
Finland | 3 |
6量词操纵符
假如元素序列满足指定的前提 ,量词操纵符就返回布尔值
组合运算符 | 形貌 |
---|---|
Any | 肯定鸠合中是不是有满足谓词函数的元素 |
All | 肯定鸠合中是不是都满足谓词函数的元素 |
Contains | 搜检某个元素是不是存在鸠合中 |
List<int> intList1 = new List<int>(5) {1, 2, 3, 4, 5}; bool any3 = intList1.Any(r => r == 3);//肯定鸠合中是不是有3,返回true bool any0 = intList1.Any(r => r == 0);//肯定鸠合中是不是有0,返回false bool allMoreZero = intList1.All(r => r > 0);//肯定序列中一切元素都满足大于0,返回true bool allMore2 = intList1.All(r => r > 2); //返回false bool contains3 = intList1.Contains(3); //元素3是不是在鸠合中,true
7分区操纵符
分区操纵符,返回一个子集。运用它们能够获得部份效果。
分区运算符 | 形貌 |
---|---|
Take | 必需制订提取元素的个数 |
Skip | 跳过指定的元素个数,提取其他元素 |
TakeWhile | 提取前提为真的元素 |
SkipWhile | 跳过前提为真的元素 |
int pageSize = 5; int numberPages = (int)Math.Ceiling(Formulal.Racers.Count() / (double)pageSize); for (int page = 0; page < numberPages; page++) { var racersPartition = (from r in Formulal.Racers orderby r.LastName, r.FirstName select r.FirstName + " " + r.LastName).Skip(page * pageSize).Take(pageSize); }
效果:
Page 0Fernando Alons Mario Andretti Alberto Ascari Jack Brabham Jenson ButtonPage 1Jim Clark Juan Manuel Fa Nino Farina Emerson Fittipaldi Mika Hakkinen Page 2Lewis Hamilton Mike Hawthorn Damon Hill Graham Hill Phil Hill
8鸠合操纵符(Set operators)
鸠合操纵符返回 一 个鸠合。除了Distinct以外,其他都须要2个鸠合。
鸠合操纵符 | 形貌 |
---|---|
Distinct | 从鸠合中删除反复的元素 |
Union | 返回出现在个中一个鸠合中的唯一元素 |
Intersect | 交集 |
Except | 只出现在一个鸠合中的元素 |
Zip | 经由过程运用指定的谓词函数兼并2个鸠合 |
运用举例:
List<int> intList1 = new List<int>(5) {1, 2, 3, 4, 5,3}; List<int> intList2 = new List<int>(3) {2, 5, 0}; var delDuplic = intList1.Distinct(); //去重,效果{1,2,3,4,5} var unionList = intList1.Union(intList2); //并集,效果{1,2,3,4,5,0} var interList = intList1.Intersect(intList2); //交集,效果{2,5} var exceptList = intList1.Except(intList2);//差集,效果{1,3,4} var exceptList2 = intList2.Except(intList1);//差集,效果{0} var unionList2 = intList1.Zip(intList2,((i1, i2) => i1+", "+i2)); 效果:{{1,2}{2,5}{3,0}}
9元素操纵符(Element operators)
这些元素操纵符仅返回一 个元素。
元素操纵符 | 形貌 |
---|---|
First | 返回第一个满足前提的元素 |
FirstOrDefault | 返回第一个满足前提的元素,但假如没有找到满足前提的元素,就返回范例的默许值 |
Last | 返回末了一个满足前提的元素 |
LastOrDefault | 返回末了一个满足前提的元素,但假如没有找到满足前提的元素,就返回范例的默许值 |
ElementAt | 指定了要返回的元素的位置 |
ElementAtOrDefault | 指定了要返回的元素的位置,但假如没有找到满足前提的元素,就返回范例的默许值 |
Single | 只返回一个满足前提的元素,假如有多个满足前提,就抛出一个非常。 |
SingleOrDefault | 只返回一个满足前提的元素,但假如没有找到满足前提的元素,就返回范例的默许值 |
运用举例:
List<int> intList1 = new List<int>(5) { 1, 8, 3, 4, 5, 3 }; int firstMore1 = intList1.First(r => r > 1); //效果:8int firstMore17 = intList1.FirstOrDefault(r => r > 17); //效果:0(由于不存在)int lastMore3 = intList1.Last(r => r > 3); //效果5int last = intList1.ElementAt(2); //效果3,(索引从0最先)
10聚合操纵符(Aggregate operators)
聚合操纵符盘算鸠合的一个值。应用这些聚合操纵符 ,可 以盘算一切值的总和、一切元
素的个数、值最大和最小的元素 ,以 及平均值等
聚合操纵符 | 形貌 |
---|---|
Count | 一切值的个数 |
Sum | 一切值的综合 |
Min | 一切值的的最小值 |
Max | 一切值的的最大值 |
Average | 一切值的平均数 |
public class MyIntClass { public string Name { get; set; } public int Integar { get; set; } public MyIntClass(string name, int i) { this.Name = name; this.Integar = Integar; } } List<MyIntClass> intList1 = new List<MyIntClass>(5) { new MyIntClass("first",1), new MyIntClass("second",8) , new MyIntClass("third",3) , new MyIntClass("fourth",4) , new MyIntClass("fifth",5) , new MyIntClass("sixth",3) }; int count = intList1.Count; int sum = intList1.Sum(r=>r.Integar); int min = intList1.Min(r => r.Integar); int max = intList1.Max(r => r.Integar); double average = intList1.Average(r => r.Integar);
11转换操纵符(Conversion operators)
转换操纵符将鸠合转换为数组 :IEnumberable、 IList, IDictionary 等。
Conversion operators | 形貌 |
---|---|
ToArray | 鸠合转化为Array |
AsEnumerable | 返回范例为IEnumerable< T> |
ToList | 鸠合转化为List |
ToDictionary | 鸠合转化为Dictionary |
Cast< TResult> | 映照 |
照样上面的例子
IEnumerable<MyIntClass> ienuList = from r in intList1 where r.Integar > 3 select r; //返回默许的IEnumerable鸠合 List<MyIntClass> ienuList2 = (from r in intList1 where r.Integar > 3 select r).ToList(); //返回List MyIntClass[] ienuList2 = (from r in intList1 where r.Integar > 3 select r).ToArray();//返回数组 var dict = (from r in intList1 where r.Integar > 3 select r).ToDictionary(r=>r.Name,r=>r.Integar); //字典,key是name, value:Integar IEnumerable<MyIntClass> ienuList2 = (from r in intList1 where r.Integar > 3 select r).AsEnumerable();
12生成操纵符(Generation operators)
这些生成操纵符返回 一 个新鸠合
Generation operators | 形貌 |
---|---|
Empty | 鸠合是空的 |
Range | 返回一系列数字 |
Repeat | 返回一直反复一个值的鸠合 |
IEnumerable<int> ints = Enumerable.Range(3, 10); //{3,4,5,6,7,8,9,10,11,12}IEnumerable<int> emptyInts = Enumerable.Empty<int>(); //生成一个空鸠合IEnumerable<int> ints2= Enumerable.Repeat(6,8); //生成8个6的鸠合
附:展现所用到的实体类和数据
选手实体类
//选手实体类 public class Racer { public Racer(string firstName = null, string lastName = null, string country = null, int starts = 0,int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null) { this.FirstName = firstName; this.LastName = lastName; this.Country = country; this.Starts = starts; this.Wins = wins; var yearList = new List<int>(); if (years != null) { foreach (var year in years) { yearList.Add(year); } this.Years = yearList.ToArray(); } if (cars != null) { var carList = new List<string>(); foreach (var car in cars) { carList.Add(car); } this.Cars = carList.ToArray(); } public string FirstName { get; set; } public string LastName { get; set; } //博得竞赛的次数 public int Wins { get; set; } //所属国度 public string Country { get; set; } //最先做的岁数 public int Starts { get; set; } //车型数组 public string[] Cars { get; private set; } //博得冠军的年份 public int[] Years { get; private set; } } }
选手数据
//选手实体类 public class Racer { public Racer(string firstName = null, string lastName = null, string country = null, int starts = 0,int wins = 0, IEnumerable<int> years = null, IEnumerable<string> cars = null) { this.FirstName = firstName; this.LastName = lastName; this.Country = country; this.Starts = starts; this.Wins = wins; var yearList = new List<int>(); if (years != null) { foreach (var year in years) { yearList.Add(year); } this.Years = yearList.ToArray(); } if (cars != null) { var carList = new List<string>(); foreach (var car in cars) { carList.Add(car); } this.Cars = carList.ToArray(); } public string FirstName { get; set; } public string LastName { get; set; } //博得竞赛的次数 public int Wins { get; set; } //所属国度 public string Country { get; set; } //最先做的岁数 public int Starts { get; set; } //车型数组 public string[] Cars { get; private set; } //博得冠军的年份 public int[] Years { get; private set; } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 选手数据 //选手List public static List<Racer> Racers = new List<Racer>(40) { new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo" }), new Racer("Alberto", "Ascari", "Italy", 32, 10, new int[] { 1952, 1953 }, new string[] { "Ferrari" }), new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }), new Racer("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" }), new Racer("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" }), new Racer("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" }), new Racer("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" }), new Racer("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" }), new Racer("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" }), new Racer("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" }), new Racer("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" }), new Racer("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" }), new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" }), new Racer("James", "Hunt", "UK", 91, 10, new int[] { 1976 }, new string[] { "McLaren" }), new Racer("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" }), new Racer("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" }), new Racer("Alan", "Jones", "Australia", 115, 12, new int[] { 1980 }, new string[] { "Williams" }), new Racer("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" }), new Racer("Niki", "Lauda", "Austria", 173, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" }), new Racer("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" }), new Racer("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" }), new Racer("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" }), new Racer("Alain", "Prost", "France", 197, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" }), new Racer("Damon", "Hill", "UK", 114, 22, new int[] { 1996 }, new string[] { "Williams" }), new Racer("Jacques", "Villeneuve", "Canada", 165, 11, new int[] { 1997 }, new string[] { "Williams" }), new Racer("Mika", "Hakkinen", "Finland", 160, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" }), new Racer("Michael", "Schumacher", "Germany", 287, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }), new Racer("Fernando", "Alonso", "Spain", 252, 32, new int[] { 2005, 2006 }, new string[] { "Renault" }), new Racer("Kimi", "Räikkönen", "Finland", 230, 20, new int[] { 2007 }, new string[] { "Ferrari" }), new Racer("Lewis", "Hamilton", "UK", 166, 43, new int[] { 2008, 2014, 2015 }, new string[] { "McLaren", "Mercedes" }), new Racer("Jenson", "Button", "UK", 283, 15, new int[] { 2009 }, new string[] { "Brawn GP" }), new Racer("Sebastian", "Vettel", "Germany", 156, 42, new int[] { 2010, 2011, 2012, 2013 }, new string[] { "Red Bull Racing" }) };
团队实体类
[Serializable] public class Team { public Team(string name, params int[] years) { this.Name = name; this.Years = years; } public string Name { get; private set; } public int[] Years { get; private set; } }
团队数据
//冠军团队List public static List<Team> ChampionTeams = new List<Team>() { new Team("Vanwall", 1958), new Team("Cooper", 1959, 1960), new Team("Ferrari", 1961, 1964, 1975, 1976, 1977, 1979, 1982, 1983, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008), new Team("BRM", 1962), new Team("Lotus", 1963, 1965, 1968, 1970, 1972, 1973, 1978), new Team("Brabham", 1966, 1967), new Team("Matra", 1969), new Team("Tyrrell", 1971), new Team("McLaren", 1974, 1984, 1985, 1988, 1989, 1990, 1991, 1998), new Team("Williams", 1980, 1981, 1986, 1987, 1992, 1993, 1994, 1996, 1997), new Team("Benetton", 1995), new Team("Renault", 2005, 2006), new Team("Brawn GP", 2009), new Team("Red Bull Racing", 2010, 2011, 2012, 2013), new Team("Mercedes", 2014, 2015) };
以上就是C#Linq查询操纵运用举例的内容,更多相干内容请关注ki4网(www.ki4.cn)!