C# 單值選擇查詢
在SQL數(shù)據(jù)查詢語言中,我們熟悉的另一類查詢是SELECT DISTINCT查詢,該査詢可搜索數(shù)據(jù)中的唯一值,也就是說,值是不重復(fù)的。這是使用查詢時的一個常見需求。
假定需要在前面示例使用的顧客數(shù)據(jù)中査找不同的區(qū)域,由于在這些數(shù)據(jù)中沒有單獨(dú)的區(qū)域列表,所以需要從顧客列表中找出唯一的、不重復(fù)的區(qū)域列表。LINQ提供了 DistinctQ方法,以便找出這些數(shù)據(jù),如下面的示例所示。
試一拭 投影——單值選擇查詢:BeginningCSharp7_22_8_SelectDistinctQuery\Program.cs
按照下面的步驟在Visual Studio2017中創(chuàng)建示例:
(1)在 C:\BeginningCSharp7\Chapter22目錄中創(chuàng)建一個新的控制臺應(yīng)用程序 BeginningCSharp7_22_8. SelectDistinctQuery。
(2)輸入如下代碼,創(chuàng)建 Customer 類,初始化 customers 列表(List<Customer> customers):
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return "ID: " + ID + " City: " + City +
" Country: " + Country +
" Region: " + Region +
" Sales: " + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer> {
new Customer { ID="A",City="New York", Country="USA",
Region="North America", Sales=9999},
new Customer { lD="B", City="Mumbai", Country="India",
Region="Asia", Sales=8888},
new Customer { ID="C", City="Karachi", Country="Pakistan",
Region="Asia", Sales=7777},
new Customer { ID="D", City="Delhi", Country="India",
Region="Asia", Sales=6666},
new Customer { ID="E", City="Sao Paulo", Country="Brazil",
Region="South America", Sales=5555 },
new Customer { ID="F", City="Moscow", Country="Russia",
Region="Europe", Sales=4444 },
new Customer { ID="G", City="Seoul", County="Korea",
Region="Asia", Sales=3333 },
new Customer { ID="H", City="Istanbul", County="Turkey",
Region="Asiar", Sales=2222 },
new Customer { ID="I", City="Shanghai", Country="China",
Region=,,Asia", Sales=llll },
new Customer { ID="J", City="Lagos", Country="Nigeria",
Region="Africa", Sales=1000 },
new Customer { ID="K", City="Mexico City", Country="Mexico",
Region="North America", Sales=2000 }
new Customer { ID="L", City="Jakarta", Country="Indonesia",
Region="Asia", Sales=3000 },
new Customer { ID="M", City="Tokyo", Country="Japan",
Region="Asia", Sales=4000 },
new Customer { ID="N", City="Los Angeles", Country="USAt",
Region="North America", Sales=5000 }
new Customer { ID="O", City="Cairo", Country="Egypt",
Region="Africa", Sales=6000 },
new Customer { ID="P", City="Tehran", Country="Iran",
Region="Asia", Sales=7000 },
new Customer { ID="Q", City="London", Country="UK",
Region="Europe", Sales=8000 },
new Customer { ID="R", City="Beijing", Country="China",
Region="Asia", Sales=9000 },
new Customer { ID="S",City="Bogota", Country="Colombia",
Region="South America", Sales=1001 },
new Customer { ID="T", City="Lima", Country="Peru",
Region="South America", Sales=2002 }
};
(3)在Main()方法的customers列表初始化之后,輸入(或修改)查詢,如下所示:
var queryResults = customers.Select(c => c.Region).Distinct();
(4)完成MainO方法的其余代碼,如下所示:
foreach (var item in queryResults)
{
WriteLine{item);
}
Write("Program finished, press Enter/Return to continue:");
ReadLine();
(5)編譯并執(zhí)行程序,結(jié)果顯示的是顧客所在的唯一區(qū)域,如下所示:
North America
Asia
South America
Europe
Africa
Program finished, press Enter/Return to continue:
示例說明
Customer類和customers列表的初始化與前面例子中的相同。在查詢語句中,調(diào)用了Select()方法,用一個簡單的Lambda表達(dá)式從Customer對象中選擇區(qū)域,再調(diào)用Distinct(),從Select()中返回唯一的結(jié)果:
var queryResults = customers.Select(c => c.Region).Distinct();
只能在方法語法中使用Distinct(),所以使用方法語法調(diào)用Select還可以調(diào)用Distinct()來修改在查詢語法中創(chuàng)建的查詢:
var queryResults = (from c in customers select c.Region).Distinct();
查詢語法由C#編譯器轉(zhuǎn)換為方法語法中的同系列LINQ方法調(diào)用,所以如果可以改進(jìn)可讀性和代碼風(fēng)格, 可以混合和匹配它們。
點(diǎn)擊加載更多評論>>