C#的Linq 語句的使用

Linq 概述

Linq(語句集成查詢) 在C#中集成了查詢語法,可以使用相同的語法訪問不同的數據源,Linq集成了不同數據源的抽象層,所以可以使用相同的語法

下面放上例子

賽車手類

public class Racer : IComparable<Racer>, IFormattable {
        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 IEnumerable<string> cars { get; set; }
        //贏的年份
        public IEnumerable<int> years { get; set; }

        public Racer(string firstname,string lastname,string country,int starts,int 
        wins):this(firstname,lastname,country,starts,wins,null,null)
        {

        }
        


        public Racer(string firstname,string lastname,string country,int starts,int wins,IEnumerable<int> years,IEnumerable<string> cars)
        {
            this.firstname = firstname;
            this.lastname = lastname;
            this.wins = wins;
            this.country = country;
            this.starts = starts;
            this.years = years;
            this.cars = cars;
        }

        public int CompareTo (Racer other) {
            return lastname.CompareTo (other?.lastname);
        }

        public override string ToString () => $"{firstname},{lastname}";

        public string ToString (string format) => ToString (format, null);

        public string ToString (string format, IFormatProvider formatProvider) {

            switch (format) {
                case null:
                case "N":
                    return ToString ();
                case "F":
                    return firstname;
                case "L":
                    return lastname;
                case "C":
                    return country;
                case "S":
                    return starts.ToString ();
                case "W":
                    return wins.ToString ();
                case "A":
                    return $"{firstname},{lastname},{country},Starts:{starts},Wins:     
                    {wins}";
                default:
                    throw new FormatException ($"Format : {format} not supposed");
            }
        }

    }

數據源類(其中數據只是作爲測試,直接複製即可)

 public class Formulal
  {
        private static List<Racer> _racers;

        //返回所有贏的賽車手
        public static IList<Racer> GetChampions()
        {
            if(_racers == null)
            {

                _racers = new List<Racer>(40);
                _racers.Add(new Racer("Nino", "Farina", "Italy", 33, 5, new int[] { 1950 }, new string[] { "Alfa Romeo" }));
                _racers.Add(new Racer("Alberto", "Ascari", "Italy", 32, 10, new int[] { 1952, 1953 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", 51, 24, new int[] { 1951, 1954, 1955, 1956, 1957 }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));
                _racers.Add(new Racer("Mike", "Hawthorn", "UK", 45, 3, new int[] { 1958 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("Phil", "Hill", "USA", 48, 3, new int[] { 1961 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("John", "Surtees", "UK", 111, 6, new int[] { 1964 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("Jim", "Clark", "UK", 72, 25, new int[] { 1963, 1965 }, new string[] { "Lotus" }));
                _racers.Add(new Racer("Jack", "Brabham", "Australia", 125, 14, new int[] { 1959, 1960, 1966 }, new string[] { "Cooper", "Brabham" }));
                _racers.Add(new Racer("Denny", "Hulme", "New Zealand", 112, 8, new int[] { 1967 }, new string[] { "Brabham" }));
                _racers.Add(new Racer("Graham", "Hill", "UK", 176, 14, new int[] { 1962, 1968 }, new string[] { "BRM", "Lotus" }));
                _racers.Add(new Racer("Jochen", "Rindt", "Austria", 60, 6, new int[] { 1970 }, new string[] { "Lotus" }));
                _racers.Add(new Racer("Jackie", "Stewart", "UK", 99, 27, new int[] { 1969, 1971, 1973 }, new string[] { "Matra", "Tyrrell" }));
                _racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", 143, 14, new int[] { 1972, 1974 }, new string[] { "Lotus", "McLaren" }));
                _racers.Add(new Racer("James", "Hunt", "UK", 91, 10, new int[] { 1976 }, new string[] { "McLaren" }));
                _racers.Add(new Racer("Mario", "Andretti", "USA", 128, 12, new int[] { 1978 }, new string[] { "Lotus" }));
                _racers.Add(new Racer("Jody", "Scheckter", "South Africa", 112, 10, new int[] { 1979 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("Alan", "Jones", "Australia", 115, 12, new int[] { 1980 }, new string[] { "Williams" }));
                _racers.Add(new Racer("Keke", "Rosberg", "Finland", 114, 5, new int[] { 1982 }, new string[] { "Williams" }));
                _racers.Add(new Racer("Niki", "Lauda", "Austria", 173, 25, new int[] { 1975, 1977, 1984 }, new string[] { "Ferrari", "McLaren" }));
                _racers.Add(new Racer("Nelson", "Piquet", "Brazil", 204, 23, new int[] { 1981, 1983, 1987 }, new string[] { "Brabham", "Williams" }));
                _racers.Add(new Racer("Ayrton", "Senna", "Brazil", 161, 41, new int[] { 1988, 1990, 1991 }, new string[] { "McLaren" }));
                _racers.Add(new Racer("Nigel", "Mansell", "UK", 187, 31, new int[] { 1992 }, new string[] { "Williams" }));
                _racers.Add(new Racer("Alain", "Prost", "France", 197, 51, new int[] { 1985, 1986, 1989, 1993 }, new string[] { "McLaren", "Williams" }));
                _racers.Add(new Racer("Damon", "Hill", "UK", 114, 22, new int[] { 1996 }, new string[] { "Williams" }));
                _racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 165, 11, new int[] { 1997 }, new string[] { "Williams" }));
                _racers.Add(new Racer("Mika", "Hakkinen", "Finland", 160, 20, new int[] { 1998, 1999 }, new string[] { "McLaren" }));
                _racers.Add(new Racer("Michael", "Schumacher", "Germany", 287, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }));
                _racers.Add(new Racer("Fernando", "Alonso", "Spain", 273, 33, new int[] { 2005, 2006 }, new string[] { "Renault" }));
                _racers.Add(new Racer("Kimi", "Räikkönen", "Finland", 253, 20, new int[] { 2007 }, new string[] { "Ferrari" }));
                _racers.Add(new Racer("Lewis", "Hamilton", "UK", 189, 53, new int[] { 2008, 2014, 2015 }, new string[] { "McLaren", "Mercedes" }));
                _racers.Add(new Racer("Jenson", "Button", "UK", 306, 16, new int[] { 2009 }, new string[] { "Brawn GP" }));
                _racers.Add(new Racer("Sebastian", "Vettel", "Germany", 179, 42, new int[] { 2010, 2011, 2012, 2013 }, new string[] { "Red Bull Racing" }));
                _racers.Add(new Racer("Nico", "Rosberg", "Germany", 207, 24, new int[] { 2016 }, new string[] { "Mercedes" }));
            }
            return _racers;
        }
}

使用方法

using System;
using System.Linq;
using Program;

 public class MainTest
    {
        static void Main()
        {
            var query = from r in Formulal.GetChampions() 
                        where r.country == "Brazil" 
                        orderby r.wins descending select r;
            foreach(var r in query)
            {
                Console.WriteLine($"{r:A}");
            }
        }
    }

注意:變量query只是指定了Linq查詢,該查詢不是通過這個賦值語句執行的,是通過foreach循環訪問查詢,該查詢就會執行。

 

from r in Formulal.GetChampions() where r.country == "Brazil" orderby r.wins descending select r;                   

這是一個典型的Linq查詢,子句 from where orderby descending select 都是查詢的關鍵字,查詢語句都是用form 開頭 後接 需要查詢的數據源,然後以select 或者group 結尾,在from 和 (select/group)之間可以用 where ,orderby, join, let 等關鍵字加入查詢的條件

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章