基本概念
LINQ(Language Integrated Query) :LINQ(Language Integrated Query)語言集成查詢是一組用於c#和Visual Basic語言的擴展。它允許編寫C#或者Visual Basic代碼以操作內存數據的方式,查詢數據庫。
問題描述
已有Racer類和冠軍車手數據,進行以下查詢,並打印結果:
1)查詢來自英國(UK)的所有世界冠軍,並按勝利場數降序排列。
輸出結果時,可用Console.WriteLine("{0:A}", racer)或WriteLine($"{racer:A}");。
2)使用LINQ方法語法重寫這個查詢。
3)體會LINQ的推遲查詢:首先建立查詢query2,查出所有勝利場數超過25,用foreach循環並打印;然後將所有英國(UK)車手的國名改爲“United Kingdom”;再次對query2用foreach循環並打印。觀賽同一查詢的不同執行結果。
4)查出首發場數超過100,並且勝利場數超過20的車手的姓名(包含名和姓)。
5)基於現有數據,統計德國人的勝利總場數。
6)進行投影查詢,對於駕馭法拉利(Ferrari)車型奪冠的車手,輸出人名、國別與首發勝率,人名包含名和姓,首發勝率爲Wins除以Starts。
源代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework8
{
public class Racer : IComparable<Racer>, IFormattable
{
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 yearsList = new List<int>();
foreach (var year in years)
{
yearsList.Add(year);
}
this.Years = yearsList.ToArray();
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 string Country { get; set; }
public int Wins { get; set; }//奪冠場數
public int Starts { get; set; }//首發場數
public string[] Cars { get; private set; }//賽車手獲得冠軍那一年使用的所有車型
public int[] Years { get; private set; }//賽車手獲得冠軍的年份
public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
public int CompareTo(Racer other)
{
if (other == null) throw new ArgumentNullException("other");
return this.LastName.CompareTo(other.LastName);
}
public string ToString(string format)
{
return 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 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 static IList<Racer> GetChampions()//1950-2008年一級方程式錦標賽冠軍
{
IList<Racer> 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", 250, 91, new int[] { 1994, 1995, 2000, 2001, 2002, 2003, 2004 }, new string[] { "Benetton", "Ferrari" }));
racers.Add(new Racer("Fernando", "Alonso", "Spain", 132, 21, new int[] { 2005, 2006 }, new string[] { "Renault" }));
racers.Add(new Racer("Kimi", "R?ikk?nen", "Finland", 148, 17, new int[] { 2007 }, new string[] { "Ferrari" }));
racers.Add(new Racer("Lewis", "Hamilton", "UK", 44, 9, new int[] { 2008 }, new string[] { "McLaren" }));
return racers;
}
}
/*
利用LINQ技術查詢Racer對象
已有Racer類和冠軍車手數據,進行以下查詢,並打印結果:
1)查詢來自英國(UK)的所有世界冠軍,並按勝利場數降序排列。
輸出結果時,可用Console.WriteLine("{0:A}", racer)或WriteLine($"{racer:A}");。
2)使用LINQ方法語法重寫這個查詢。
3)體會LINQ的推遲查詢:首先建立查詢query2,查出所有勝利場數超過25,用foreach循環並打印;然後將所有英國(UK)車手的國名改爲“United Kingdom”;再次對query2用foreach循環並打印。觀賽同一查詢的不同執行結果。
4)查出首發場數超過100,並且勝利場數超過20的車手的姓名(包含名和姓)。
5)基於現有數據,統計德國人的勝利總場數。
6)進行投影查詢,對於駕馭法拉利(Ferrari)車型奪冠的車手,輸出人名、國別與首發勝率,人名包含名和姓,首發勝率爲Wins除以Starts。 */
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1)查詢來自英國(UK)的所有世界冠軍,並按勝利場數降序排列。");
List<Racer> list = new List<Racer>(Racer.GetChampions());
list.Sort((a,b) => {
return b.Wins - a.Wins;
});
foreach (var racer in list)
{
if(racer.Country == "UK")
Console.WriteLine("{0:A}", racer);
}
Console.WriteLine("2)使用LINQ方法語法重寫這個查詢。");
var query1 = from r in Racer.GetChampions()
where r.Country == "UK"
orderby r.Wins descending
select r;
foreach (var racer in query1)
{
Console.WriteLine("{0:A}", racer);
}
Console.WriteLine("3)體會LINQ的推遲查詢:首先建立查詢query2,查出所有勝利場數超過25,用foreach循環並打印;然後將所有英國(UK)車手的國名改爲“United Kingdom”;再次對query2用foreach循環並打印。觀賽同一查詢的不同執行結果。");
var query2 = from r in Racer.GetChampions()
where r.Wins > 25
orderby r.Wins descending
select r;
foreach (var racer in query2)
{
Console.WriteLine("{0:A}", racer);
if (racer.Country == "UK")
racer.Country = "United Kingdom";
}
foreach (var racer in query2)
{
Console.WriteLine("{0:A}", racer);
}
Console.WriteLine("4)查出首發場數超過100,並且勝利場數超過20的車手的姓名(包含名和姓)。");
var query3 = from r in Racer.GetChampions()
where r.Starts >100
where r.Wins > 20
orderby r.Wins descending
select r.FirstName + " " + r.LastName;
foreach (var racer in query3)
{
Console.WriteLine("{0:A}", racer);
}
Console.WriteLine("5)基於現有數據,統計德國人的勝利總場數。");
var query4 = from r in Racer.GetChampions()
where r.Country == "Germany"
orderby r.Wins descending
select r.Wins;
Console.WriteLine(query4.Sum());
Console.WriteLine("6)進行投影查詢,對於駕馭法拉利(Ferrari)車型奪冠的車手,輸出人名、國別與首發勝率,人名包含名和姓,首發勝率爲Wins除以Starts。");
var query5 = from r in Racer.GetChampions()
from c in r.Cars
where c == "Ferrari"
orderby r.Wins descending
select new { name = r.FirstName + " " + r.LastName , Country=r.Country, rate=(double)r.Wins/r.Starts };
foreach (var racer in query5)
{
Console.WriteLine("{0:A}", racer);
}
}
}
}
運行結果
參考文章
https://www.pianshen.com/article/159246876/
https://www.cnblogs.com/hackpig/archive/2010/02/15/1668448.html