接下来我将详细讲解C# Linq的GroupJoin()方法的使用。
GroupJoin()方法概述
GroupJoin()是Linq方法之一,其作用是将两个序列进行分组,并同时返回每个组中匹配项的列表。通俗来说,GroupJoin()方法将两个序列进行左连接,将左边序列的每一项和右边序列的匹配项分组生成新的序列,并返回这个新序列。
GroupJoin()方法的语法为:
var groupJoinResults =
from outerItem in outerSequence
join innerItem in innerSequence
on outerKey equals innerKey into myGroup
select new { Outer = outerItem, Innergroup = myGroup};
其中,outerSequence和innerSequence分别表示外层查询和内层查询的序列,分别调用Join()方法时的第一个参数。
on后面的outerKey和innerKey分别指定了两个序列要连接的键值,并且这两个变量必须具有相同的类型。进入到into之后,则定义了一个组(group)的名称,我们可以随便选择一个有效的变量名称。最后,select子句则将生成新序列的方式显式地定义出来。
GroupJoin()方法示例
现在,我将提供两个使用GroupJoin()方法的示例。
示例一
假设我们有一个学生(students)列表和一个城市(cities)列表,每个学生都有所属城市的城市ID,我们想要以城市为分组,统计每个城市的学生数量。
我们可以通过以下代码实现此功能:
// 创建城市列表和学生列表
List<City> cities = new List<City>()
{
new City { ID = 1, Name = "New York" },
new City { ID = 2, Name = "London" },
new City { ID = 3, Name = "Rio De Janeiro" }
};
List<Student> students = new List<Student>()
{
new Student { ID = 1, FirstName = "John", LastName = "Doe", CityID = 1 },
new Student { ID = 2, FirstName = "Jane", LastName = "Doe", CityID = 1 },
new Student { ID = 3, FirstName = "Bob", LastName = "Clark", CityID = 2},
new Student { ID = 4, FirstName = "Mary", LastName = "Jones", CityID = 2 },
new Student { ID = 5, FirstName = "Joe", LastName = "Smith", CityID = 3 }
};
// 使用GroupJoin()方法进行分组统计
var query = cities.GroupJoin(students,
city => city.ID,
student => student.CityID,
(city, studentGroup) => new
{ CityName = city.Name,
StudentCount = studentGroup.Count() });
// 打印结果
foreach (var item in query)
{
Console.WriteLine("{0}: {1}", item.CityName, item.StudentCount);
}
运行结果如下:
New York: 2
London: 2
Rio De Janeiro: 1
示例二
假设我们现在有一个订单(order)列表和一个消费者(customer)列表,每个订单都有一个客户ID,我们想要查询每个客户订单数量的分组并且将详细的订单信息也打印出来。
我们可以通过以下代码实现此功能:
// 创建客户列表和订单列表
List<Customer> customers = new List<Customer>()
{
new Customer { ID = 1, Name = "Alice" },
new Customer { ID = 2, Name = "Bob" },
new Customer { ID = 3, Name = "Charlie" }
};
List<Order> orders = new List<Order>()
{
new Order { ID = 1, CustomerID = 1, Amount = 99.99f },
new Order { ID = 2, CustomerID = 1, Amount = 15.50f },
new Order { ID = 3, CustomerID = 2, Amount = 800.00f},
new Order { ID = 4, CustomerID = 2, Amount = 5.00f },
new Order { ID = 5, CustomerID = 1, Amount = 150.00f },
new Order { ID = 6, CustomerID = 3, Amount = 500.00f }
};
// 使用GroupJoin()方法进行分组并打印结果
var query = customers.GroupJoin(orders,
customer => customer.ID,
order => order.CustomerID,
(customer, orderGroup) => new
{
CustomerName = customer.Name,
OrderCount = orderGroup.Count(),
Orders = orderGroup
});
foreach (var item in query)
{
Console.WriteLine(item.CustomerName + ": " + item.OrderCount);
foreach (var order in item.Orders)
{
Console.WriteLine("Order ID: {0},Amount: {1}", order.ID, order.Amount);
}
}
运行结果如下:
Alice: 3
Order ID: 1,Amount: 99.99
Order ID: 2,Amount: 15.5
Order ID: 5,Amount: 150
Bob: 2
Order ID: 3,Amount: 800
Order ID: 4,Amount: 5
Charlie: 1
Order ID: 6,Amount: 500
这两个示例展示了GroupJoin()方法在不同场景下的应用,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# Linq的GroupJoin()方法 – 将两个序列中的元素联接在一起,并根据指定的键对结果进行分组 - Python技术站