首頁 > 軟體

C#使用LINQ查詢操作符範例程式碼

2022-06-06 18:00:34

範例業務背景介紹

範例參考《C#高階程式設計(第六版)》LINQ章節(P267 - P296),進行改編。

開啟範例程式碼我們看到:

1、Racer.cs 車手冠軍資訊類

定義一級方程式世界車手冠軍資訊。    

2、Team.cs 車隊冠軍資訊類

定義一級方程式世界車隊冠軍資訊。

3、Formula1.cs 一級方程式類

包含兩個重要靜態方法:(F1是"Formula One"的縮寫)

  • GetChampions():返回一組車手列表。這個列表包含了1950到2007年之間的所有一級方程式世界車手冠軍
  • GetContructorChampions():返回一組車佇列表。這個列表包含了1985到2007年之間的所有一級方程式世界車隊冠軍。
[Serializable]
public class Racer : IComparable<Racer>, IFormattable, IEquatable<Racer>
{
    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; set; }
    public int[] Years { get; set; }


    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 String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",
                   FirstName, LastName, Country, Starts, Wins);
            default:
                throw new FormatException(String.Format(
                   "Format {0} not supported", format));
        }
    }

    public bool Equals(Racer other)
    {
        return this.FirstName == other.FirstName && this.LastName == other.LastName;
    }


    public int CompareTo(Racer other)
    {
        return this.LastName.CompareTo(other.LastName);
    }

    public string ToString(string format)
    {
        return ToString(format, null);
    }

    public override string ToString()
    {
        return String.Format("{0} {1}", FirstName, LastName);
    }
}

public class Team
{
    public string Name { get; set; }
    public int[] Years { get; set; }
}

public class Formula1
{
    //返回車手冠軍列表
    public static List<Racer> GetChampions()
    {
        List<Racer> racers = new List<Racer>(40);
        racers.Add(new Racer() { FirstName = "Nino", LastName = "Farina", Country = "Italy", Starts = 33, Wins = 5, Years = new int[] { 1950 }, Cars = new string[] { "Alfa Romeo" } });
        racers.Add(new Racer() { FirstName = "Alberto", LastName = "Ascari", Country = "Italy", Starts = 32, Wins = 10, Years = new int[] { 1952, 1953 }, Cars = new string[] { "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Juan Manuel", LastName = "Fangio", Country = "Argentina", Starts = 51, Wins = 24, Years = new int[] { 1951, 1954, 1955, 1956, 1957 }, Cars = new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Mike", LastName = "Hawthorn", Country = "UK", Starts = 45, Wins = 3, Years = new int[] { 1958 }, Cars = new string[] { "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Phil", LastName = "Hill", Country = "USA", Starts = 48, Wins = 3, Years = new int[] { 1961 }, Cars = new string[] { "Ferrari" } });
        racers.Add(new Racer() { FirstName = "John", LastName = "Surtees", Country = "UK", Starts = 111, Wins = 6, Years = new int[] { 1964 }, Cars = new string[] { "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Jim", LastName = "Clark", Country = "UK", Starts = 72, Wins = 25, Years = new int[] { 1963, 1965 }, Cars = new string[] { "Lotus" } });
        racers.Add(new Racer() { FirstName = "Jack", LastName = "Brabham", Country = "Australia", Starts = 125, Wins = 14, Years = new int[] { 1959, 1960, 1966 }, Cars = new string[] { "Cooper", "Brabham" } });
        racers.Add(new Racer() { FirstName = "Denny", LastName = "Hulme", Country = "New Zealand", Starts = 112, Wins = 8, Years = new int[] { 1967 }, Cars = new string[] { "Brabham" } });
        racers.Add(new Racer() { FirstName = "Graham", LastName = "Hill", Country = "UK", Starts = 176, Wins = 14, Years = new int[] { 1962, 1968 }, Cars = new string[] { "BRM", "Lotus" } });
        racers.Add(new Racer() { FirstName = "Jochen", LastName = "Rindt", Country = "Austria", Starts = 60, Wins = 6, Years = new int[] { 1970 }, Cars = new string[] { "Lotus" } });
        racers.Add(new Racer() { FirstName = "Jackie", LastName = "Stewart", Country = "UK", Starts = 99, Wins = 27, Years = new int[] { 1969, 1971, 1973 }, Cars = new string[] { "Matra", "Tyrrell" } });
        racers.Add(new Racer() { FirstName = "Emerson", LastName = "Fittipaldi", Country = "Brazil", Starts = 143, Wins = 14, Years = new int[] { 1972, 1974 }, Cars = new string[] { "Lotus", "McLaren" } });
        racers.Add(new Racer() { FirstName = "James", LastName = "Hunt", Country = "UK", Starts = 91, Wins = 10, Years = new int[] { 1976 }, Cars = new string[] { "McLaren" } });
        racers.Add(new Racer() { FirstName = "Mario", LastName = "Andretti", Country = "USA", Starts = 128, Wins = 12, Years = new int[] { 1978 }, Cars = new string[] { "Lotus" } });
        racers.Add(new Racer() { FirstName = "Jody", LastName = "Scheckter", Country = "South Africa", Starts = 112, Wins = 10, Years = new int[] { 1979 }, Cars = new string[] { "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Alan", LastName = "Jones", Country = "Australia", Starts = 115, Wins = 12, Years = new int[] { 1980 }, Cars = new string[] { "Williams" } });
        racers.Add(new Racer() { FirstName = "Keke", LastName = "Rosberg", Country = "Finland", Starts = 114, Wins = 5, Years = new int[] { 1982 }, Cars = new string[] { "Williams" } });
        racers.Add(new Racer() { FirstName = "Niki", LastName = "Lauda", Country = "Austria", Starts = 173, Wins = 25, Years = new int[] { 1975, 1977, 1984 }, Cars = new string[] { "Ferrari", "McLaren" } });
        racers.Add(new Racer() { FirstName = "Nelson", LastName = "Piquet", Country = "Brazil", Starts = 204, Wins = 23, Years = new int[] { 1981, 1983, 1987 }, Cars = new string[] { "Brabham", "Williams" } });
        racers.Add(new Racer() { FirstName = "Ayrton", LastName = "Senna", Country = "Brazil", Starts = 161, Wins = 41, Years = new int[] { 1988, 1990, 1991 }, Cars = new string[] { "McLaren" } });
        racers.Add(new Racer() { FirstName = "Nigel", LastName = "Mansell", Country = "UK", Starts = 187, Wins = 31, Years = new int[] { 1992 }, Cars = new string[] { "Williams" } });
        racers.Add(new Racer() { FirstName = "Alain", LastName = "Prost", Country = "France", Starts = 197, Wins = 51, Years = new int[] { 1985, 1986, 1989, 1993 }, Cars = new string[] { "McLaren", "Williams" } });
        racers.Add(new Racer() { FirstName = "Damon", LastName = "Hill", Country = "UK", Starts = 114, Wins = 22, Years = new int[] { 1996 }, Cars = new string[] { "Williams" } });
        racers.Add(new Racer() { FirstName = "Jacques", LastName = "Villeneuve", Country = "Canada", Starts = 165, Wins = 11, Years = new int[] { 1997 }, Cars = new string[] { "Williams" } });
        racers.Add(new Racer() { FirstName = "Mika", LastName = "Hakkinen", Country = "Finland", Starts = 160, Wins = 20, Years = new int[] { 1998, 1999 }, Cars = new string[] { "McLaren" } });
        racers.Add(new Racer() { FirstName = "Michael", LastName = "Schumacher", Country = "Germany", Starts = 250, Wins = 91, Years = new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, Cars = new string[] { "Benetton", "Ferrari" } });
        racers.Add(new Racer() { FirstName = "Fernando", LastName = "Alonso", Country = "Spain", Starts = 105, Wins = 19, Years = new int[] { 2005, 2006 }, Cars = new string[] { "Renault" } });
        racers.Add(new Racer() { FirstName = "Kimi", LastName = "Räikkönen", Country = "Finland", Starts = 122, Wins = 15, Years = new int[] { 2007 }, Cars = new string[] { "Ferrari" } });
        return racers;
    }
    //返回車隊冠軍列表
    public static List<Team> GetContructorChampions()
    {
        List<Team> teams = new List<Team>(20);
        teams.Add(new Team() { Name = "Vanwall", Years = new int[] { 1958 } });
        teams.Add(new Team() { Name = "Cooper", Years = new int[] { 1959, 1960 } });
        teams.Add(new Team() { Name = "Ferrari", Years = new int[] { 1961, 1964, 1975, 1976, 1977, 1979, 1982, 1983, 1999, 2000, 2001, 2002, 2003, 2004, 2007 } });
        teams.Add(new Team() { Name = "BRM", Years = new int[] { 1962 } });
        teams.Add(new Team() { Name = "Lotus", Years = new int[] { 1963, 1965, 1968, 1970, 1972, 1973, 1978 } });
        teams.Add(new Team() { Name = "Brabham", Years = new int[] { 1966, 1967 } });
        teams.Add(new Team() { Name = "Matra", Years = new int[] { 1969 } });
        teams.Add(new Team() { Name = "Tyrrell", Years = new int[] { 1971 } });
        teams.Add(new Team() { Name = "McLaren", Years = new int[] { 1974, 1984, 1985, 1988, 1989, 1990, 1991, 1998 } });
        teams.Add(new Team() { Name = "Williams", Years = new int[] { 1980, 1981, 1986, 1987, 1992, 1993, 1994, 1996, 1997 } });
        teams.Add(new Team() { Name = "Benetton", Years = new int[] { 1995 } });
        teams.Add(new Team() { Name = "Renault", Years = new int[] { 2005, 2006 } });

        return teams;
    }
}

一、篩選操作符

where子句合併多個表示式。 根據條件返回匹配元素的集合IEnumerable。

業務說明:找出贏得至少15場比賽的英國和奧地利賽車手。

var racers = from r in Formula1.GetChampions()
             where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria")
             select r;

foreach (var racer in racers)
{
    Console.WriteLine("{0:A}", racer);
}

下面程式碼有Where擴充套件方法Where和Select呼叫。

var racers = Formula1.GetChampions().
    Where(r => r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria")).
    Select(r => r);

結果:

//Niki Lauda, Austria; starts: 173, wins: 25 
//Nelson Piquet, Brazil; starts: 204, wins: 23 
//Ayrton Senna, Brazil; starts: 161, wins: 41

1、索引器篩選

索引是篩選器返回的每個結果的計數器。下面由Where擴充套件方法呼叫, 使用索引返回。

業務說明:找出上述的、索引為偶數的賽車手

var racers1 = Formula1.GetChampions()
     .Where((r, index)=> r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria") && index % 2 != 0)
     .Select(r => r);

結果:

Nelson Piquet

2、型別篩選OfType

基於型別篩選,使用 OfType 擴充套件方法。

業務說明:取出下面所有int型別的引數

object[] data = { "one", 1, 2, "li" };
var query = data.OfType<int>();

foreach (var intValue in query)
{
    Console.WriteLine("{0}", intValue);
}
//1
//2

例2:取出下面racer型別的值

IList mixedList = new ArrayList();
mixedList.Add( Formula1.GetChampions()[0]);
mixedList.Add(Formula1.GetContructorChampions()[0]);
mixedList.Add("Two");
mixedList.Add(3);

var query = mixedList.OfType<Racer>();

foreach (var item in query)
{
    Console.WriteLine("{0}", item);
}

二、投影操作符

1、Select 子句

將序列的每個元素經過lambda表示式處理後投影到一個新型別元素上。

(與SelectMany不同在於,若單個元素投影到IEnumerable,Select不會對多個IEnumerable進行合併)

用Select方法建立匿名型別

var query2 = Formula1.GetChampions()
     .GroupBy(r => r.Country)
     .Select(g => new { Group = g, Count = g.Count() })
     .OrderByDescending(g => g.Count)
     .ThenBy(g => g.Group.Key)
     .Where(g => g.Count > 1)
     .Select(g => new
     {
         Country = g.Group.Key,
         Count = g.Count
     });

foreach (var racer in query2)
{
    Console.WriteLine("{0:A}", racer);
}

結果:

{ Country = UK, Count = 8 } 
{ Country = Brazil, Count = 3 } 
{ Country = Finland, Count = 3 } 
{ Country = Australia, Count = 2 } 
{ Country = Austria, Count = 2 } 
{ Country = Italy, Count = 2 } 
{ Country = USA, Count = 2 }

相應的lambda表示式:

var query3 = from r in Formula1.GetChampions()
             group r by r.Country into g
             let count=g.Count()
             orderby count descending, g.Key
             where count > 1
             select new
             {
                 Country = g.Key,
                 Count = count

             };

foreach (var racer in query3)
{
    Console.WriteLine("{0:A}", racer);
}

2、複合的From子句

業務說明:篩選駕駛法拉利的所有冠軍

var ferrariDrivers = from r in Formula1.GetChampions()
                     from c in r.Cars
                     where c == "Ferrari"
                     orderby r.LastName
                     select r.FirstName + " " + r.LastName;

foreach (var racer in ferrariDrivers)
{
    Console.WriteLine("{0:A}", racer);
}

結果:

Alberto Ascari 
Juan Manuel Fangio 
Mike Hawthorn 
Phil Hill 
Niki Lauda 
Kimi Räikkönen 
Jody Scheckter 
Michael Schumacher 
John Surtees

C#編譯器會把複合的from語句轉換為SelectMany方法。

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);

符合from子句,將子集合與復集合在同一個表平行展示。

var ferrariDrivers = Formula1.GetChampions()
.SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c })
.Where(r => r.Racer.Country == "UK")
.OrderBy(r => r.Racer.LastName)
.ThenBy(r => r.Car)
.Select(r => r);


foreach (var racer in ferrariDrivers)
{
    Console.WriteLine("{0:A}", racer + " :" + racer.Racer.Cars);
}

結果:

{ Racer = Jim Clark, Car = Lotus } :Lotus 
{ Racer = Mike Hawthorn, Car = Ferrari } :Ferrari 
{ Racer = Graham Hill, Car = BRM } :BRM 
{ Racer = Graham Hill, Car = Lotus } :BRM 
{ Racer = Damon Hill, Car = Williams } :Williams 
{ Racer = James Hunt, Car = McLaren } :McLaren 
{ Racer = Nigel Mansell, Car = Williams } :Williams 
{ Racer = Jackie Stewart, Car = Matra } :Matra 
{ Racer = Jackie Stewart, Car = Tyrrell } :Matra 
{ Racer = John Surtees, Car = Ferrari } :Ferrari

三、let子句

上面的例子:

var query8 = from r in Formula1.GetChampions()
             group r by r.Country into g
             orderby g.Count() descending, g.Key
             where g.Count() > 1
             select new
             {
                 Country = g.Key,
                 Count = g.Count()
             };

上面的分組查詢Count方法呼叫了多次。使用let子句可以改變這種形式

var query10 = from r in Formula1.GetChampions()
              group r by r.Country into g
              let count = g.Count()
              orderby count descending, g.Key
              where count > 1
              select new
              {
                    Country = g.Key,
                    Count = count
              };

四、排序操作符

業務說明:來自英國的賽車手按照贏得比賽的次數進行降序排序

var query6 = from r in Formula1.GetChampions()
             where r.Country == "UK"
             orderby r.Wins descending
             select r;

foreach (var racer in query6)
{
    Console.WriteLine("{0:A}", racer);
}

結果:

Nigel Mansell, UK; starts: 187, wins: 31 
Jackie Stewart, UK; starts: 99, wins: 27 
Jim Clark, UK; starts: 72, wins: 25 
Damon Hill, UK; starts: 114, wins: 22 
Graham Hill, UK; starts: 176, wins: 14 
James Hunt, UK; starts: 91, wins: 10 
John Surtees, UK; starts: 111, wins: 6 
Mike Hawthorn, UK; starts: 45, wins: 3

使用OrderBy(),OrderByDescending方式替換掉上面的寫法:

var racers2 = Formula1.GetChampions()
    .Where(r => r.Country == "UK")
    .OrderByDescending(r => r.Wins)
    .Select(r => r);

使用ThenBy() 和 ThenByDescending() 方法繼續排序進行二次排序

業務說明:獲取車手冠軍列表,並依次按照Country升序、LastName降序、FirstName升序進行排序。擴充套件方法 Take 提取前面 10 個元素。

// 查詢表示式
var racers = (from r in Formula1.GetChampions()
              orderby r.Country, r.LastName descending, r.FirstName
              select r).Take(10);
// 方法語法
var racers1 = Formula1.GetChampions()
    .OrderBy(r => r.Country)
    .ThenByDescending(r => r.LastName)
    .ThenBy(r => r.FirstName).Take(10); 

foreach (var racer in racers)
{
    Console.WriteLine("{0:A}", racer);
}
//Juan Manuel Fangio, Argentina; starts: 51, wins: 24
//Alan Jones, Australia; starts: 115, wins: 12
//Jack Brabham, Australia; starts: 125, wins: 14
//Jochen Rindt, Austria; starts: 60, wins: 6
//Niki Lauda, Austria; starts: 173, wins: 25
//Ayrton Senna, Brazil; starts: 161, wins: 41
//Nelson Piquet, Brazil; starts: 204, wins: 23
//Emerson Fittipaldi, Brazil; starts: 143, wins: 14
//Jacques Villeneuve, Canada; starts: 165, wins: 11
//Keke Rosberg, Finland; starts: 114, wins: 5

五、分組操作符

根據一個關鍵字值對查詢結果進行分組,使用 group 子句。

子句 group r by r.Country into g 根據 Country 屬性組合。並定義一個新的識別符號g,它以後用於存取分組的結果資訊。

業務說明:列出每個國家的冠軍數

var countries = from r in Formula1.GetChampions()
                group r by r.Country into g
                orderby g.Count() descending, g.Key
                where g.Count() > 1
                select new
                {
                    Country = g.Key,
                    Count = g.Count()
                };

foreach (var country in countries)
{
    Console.WriteLine(format: "{0,-10} {1}", arg0: country.Country, arg1: country.Count);
}

結果:

UK         8 
Brazil     3 
Finland    3 
Australia  2 
Austria    2 
Italy      2 
USA        2

使用擴充套件方法,子句 group r by r.Country into g 解析為 GroupBy(r => r.Country) 返回分組序列。

var countries = Formual.GetChampions()
            .GroupBy(r => r.Country)
            .OrderByDescending(g => g.Count())
            .ThenBy(g => g.Key)
            .Where(g => g.Count() > 1)
            .Select(g => new { Country = g.Key, Count = g.Count() });

返回值為 IEnumerable。

1、對巢狀的物件分組

分組的物件包含巢狀的序列,可以改變 select 子句建立的匿名型別。

業務說明:分組查詢每個國家獲得冠軍的賽車手人數,人數必須大於3,返回國家名稱、國家賽車手冠軍數量、賽車手名稱。

var query = from r in Formula1.GetChampions()
            group r by r.Country into g
            orderby g.Count() descending, g.Key
            where g.Count() >= 3
            select new
            {
                Country = g.Key,
                count = g.Count(),
                RacerNickName = from r1 in g orderby r1.FirstName select r1.FirstName + " " + r1.LastName
            };

foreach (var g in query)
{
    Console.WriteLine("{0:A}",g.Country+" "+g.count);
    foreach (var element in  g.RacerNickName)
    {
        Console.WriteLine("---{0:A}",element);
    }
}

對應的擴充套件方法:

var query1 = Formula1.GetChampions()
    .GroupBy(r => r.Country)
    .OrderByDescending(g => g.Count())
    .ThenBy(g => g.Key)
    .Where(g => g.Count() >= 3)
    .Select(g => new
    {
        Country = g.Key,
        count = g.Count(),
        RacerNickName = g.OrderBy(r1 => r1.FirstName).Select(r1 => r1.FirstName + " " + r1.LastName)
    });

結果:

UK 8 
---Damon Hill 
---Graham Hill 
---Jackie Stewart 
---James Hunt 
---Jim Clark 
---John Surtees 
---Mike Hawthorn 
---Nigel Mansell 
Brazil 3 
---Ayrton Senna 
---Emerson Fittipaldi 
---Nelson Piquet 
Finland 3 
---Keke Rosberg 
---Kimi Räikkönen 
---Mika Hakkinen

2、多欄位分組

var query = from r in Formula1.GetChampions()
            group r by new { r.Country, r.LastName } into g
            orderby g.Count() descending, g.Key.Country, g.Key.LastName
            where g.Count() >= 1
            select new
            {
                MyGroup = g.Key,
                count = g.Count(),
                Racers = g
            };

foreach (var g in query)
{
    Console.WriteLine("{0:A}", g.MyGroup + " : " + g.count);
    foreach (var element in g.Racers)
    {
        Console.WriteLine("---{0:A}", element);
    }
}

結果:

{ Country = UK, LastName = Hill } : 2 
---Graham Hill, UK; starts: 176, wins: 14 
---Damon Hill, UK; starts: 114, wins: 22 
{ Country = Argentina, LastName = Fangio } : 1 
---Juan Manuel Fangio, Argentina; starts: 51, wins: 24

。。。

3、分組後再每組裡面僅取滿足條件的行

var query = from r in Formula1.GetChampions()
            group r by r.Country into g
            let maxId = g.Max(a => a.Wins)
            orderby g.Key
            where g.Count() >= 1
            from row in g
            where row.Wins == maxId
            select row;


foreach (var g in query)
{
    Console.WriteLine("{0:A}", g);
}

到此這篇關於C#使用LINQ查詢操作符的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com