在SQL数据查询语言中,我们熟悉的另一类查询是SELECT DISTINCT查询,该査询可搜索数据中的唯一值,也就是说,值是不重复的。这是使用查询时的一个常见需求。
假定需要在前面示例使用的顾客数据中査找不同的区域,由于在这些数据中没有单独的区域列表,所以需要从顾客列表中找出唯一的、不重复的区域列表。LINQ提供了 DistinctQ方法,以便找出这些数据,如下面的示例所示。
试一拭 投影——单值选择查询:BeginningCSharp7_22_8_SelectDistinctQuery\Program.cs
按照下面的步骤在Visual Studio2017中创建示例:
(1)在 C:\BeginningCSharp7\Chapter22目录中创建一个新的控制台应用程序 BeginningCSharp7_22_8. SelectDistinctQuery。
(2)输入如下代码,创建 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)编译并执行程序,结果显示的是顾客所在的唯一区域,如下所示:
North America
Asia
South America
Europe
Africa
Program finished, press Enter/Return to continue:
示例说明
Customer类和customers列表的初始化与前面例子中的相同。在查询语句中,调用了Select()方法,用一个简单的Lambda表达式从Customer对象中选择区域,再调用Distinct(),从Select()中返回唯一的结果:
var queryResults = customers.Select(c => c.Region).Distinct();
只能在方法语法中使用Distinct(),所以使用方法语法调用Select还可以调用Distinct()来修改在查询语法中创建的查询:
var queryResults = (from c in customers select c.Region).Distinct();
查询语法由C#编译器转换为方法语法中的同系列LINQ方法调用,所以如果可以改进可读性和代码风格, 可以混合和匹配它们。
已有 22658 名学员学习以下课程通过考试
最需教育客户端 软件问题一手掌握
去 App Store 免费下载 iOS 客户端
点击加载更多评论>>