Maison > Article > développement back-end > .NET Framework-Exemple de code utilisé par les opérateurs de classe LINQ9
Language Set Query Ganmge hteg.ratedQuw, LINQ, intègre la syntaxe de requête dans le langage de programmation C# et peut utiliser la même syntaxe pour accéder à différentes sources de données. LINQ fournit une couche d'abstraction pour différentes sources de données, afin que la même syntaxe puisse être utilisée.
L'opérateur de filtre définit les conditions de retour des éléments.
筛选操作符 | 描述 |
---|---|
where | 使用谓词,返回布尔值 |
OfType3bb63614c352f1576fbc58977cc251c2 | 根据类型筛选元素 |
Exemples d'application :
où utilisation :
var racers = from r in Formulal.Racers where r.Wins > 15 && r.Country == "Brazil" select r;
OfType24b5c9dd15451aa93c075b84f6d18944
还是上面的例子
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();
这些生成操作符返回 一 个新集合
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; } } }
//选手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) };
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!