C#Linq クエリ操作の使用例

黄舟
黄舟オリジナル
2017-03-04 10:48:051842ブラウズ


Linq 標準クエリ演算子


言語セット クエリ Ganmge hteg.ratedQuw、LINQ は、C# プログラミング言語のクエリ構文に統合されており、同じ構文を使用して異なるデータ ソースにアクセスできます。 LINQ はさまざまなデータ ソースに抽象化レイヤーを提供するため、同じ構文を使用できます。

1 フィルター演算子

フィルター演算子は、要素を返すための条件を定義します。

フィルター演算子 説明
where 述語を使用し、ブール値を返します
OfTypeb54c2c292509147c0b54128f4eb90887 型に基づいて要素をフィルターします

応用例:
使用例:

 var racers = from r in Formulal.Racers                         
 where r.Wins > 15 && r.Country == "Brazil" select r;

OfTypea7356bcf494aa12962a91b5b21b47610

ToList 集合转化为List ToDictionary 集合转化为Dictionary Cast3bb63614c352f1576fbc58977cc251c2 映射

还是上面的例子

         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查询操作使用举例的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。