📅  最后修改于: 2023-12-03 15:13:49.198000             🧑  作者: Mango
在C#中,Lambda表达式为我们提供了一种简洁的方式来查询和操作集合中的元素。本文将介绍如何使用Lambda表达式实现两个表的Join操作。
Join操作是指将两个表中符合某些条件的数据行合并到一个新的表中。Join操作通常用于将两个表中的数据进行关联,以便查询和统计数据。
在C#中,可以使用Lambda表达式来实现Join操作。Lambda表达式的语法如下:
collectionA.Join(collectionB, a => a.Key, b => b.Key, (a, b) => new { a = a, b = b });
本例中,我们使用了一个匿名类型来保存两个表中的相关数据行。在匿名类型中,我们可以定义多个属性来保存不同的数据信息。
假设我们有两个类,分别为Person
和Address
,每个类都有一个表示地址信息的属性AddressInfo
。
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string AddressInfo { get; set; }
}
public class Address
{
public int ID { get; set; }
public string AddressInfo { get; set; }
}
现在,我们需要将两个表中符合地址信息相同的元素进行Join操作。我们可以使用Lambda表达式来实现,代码如下:
var personList = new List<Person>();
personList.Add(new Person { ID = 1, Name = "张三", AddressInfo = "北京市海淀区" });
personList.Add(new Person { ID = 2, Name = "李四", AddressInfo = "上海市浦东新区" });
personList.Add(new Person { ID = 3, Name = "王五", AddressInfo = "广州市天河区" });
var addressList = new List<Address>();
addressList.Add(new Address { ID = 1, AddressInfo = "北京市海淀区" });
addressList.Add(new Address { ID = 2, AddressInfo = "上海市浦东新区" });
addressList.Add(new Address { ID = 3, AddressInfo = "广州市天河区" });
addressList.Add(new Address { ID = 4, AddressInfo = "北京市朝阳区" });
var resultList = personList.Join(addressList, p => p.AddressInfo, a => a.AddressInfo, (p, a) => new { Person = p, Address = a }).ToList();
在上述代码中,我们首先定义了两个List类型的集合personList
和addressList
。接着,我们使用Lambda表达式对这两个集合进行Join操作,指定了Join操作需要比较的属性AddressInfo
。最后,我们得到一个新的集合resultList
,其中包含了经过Join操作后的新元素。
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string AddressInfo { get; set; }
}
public class Address
{
public int ID { get; set; }
public string AddressInfo { get; set; }
}
public class Program
{
static void Main()
{
var personList = new List<Person>();
personList.Add(new Person { ID = 1, Name = "张三", AddressInfo = "北京市海淀区" });
personList.Add(new Person { ID = 2, Name = "李四", AddressInfo = "上海市浦东新区" });
personList.Add(new Person { ID = 3, Name = "王五", AddressInfo = "广州市天河区" });
var addressList = new List<Address>();
addressList.Add(new Address { ID = 1, AddressInfo = "北京市海淀区" });
addressList.Add(new Address { ID = 2, AddressInfo = "上海市浦东新区" });
addressList.Add(new Address { ID = 3, AddressInfo = "广州市天河区" });
addressList.Add(new Address { ID = 4, AddressInfo = "北京市朝阳区" });
var resultList = personList.Join(addressList, p => p.AddressInfo, a => a.AddressInfo, (p, a) => new { Person = p, Address = a }).ToList();
Console.WriteLine("合并后的数据:");
Console.WriteLine("-------------------------------");
foreach (var item in resultList)
{
Console.WriteLine("Person ID: {0}, Person Name: {1}, AddressInfo: {2}", item.Person.ID, item.Person.Name, item.Address.AddressInfo);
}
Console.ReadLine();
}
}
执行上述代码后,我们将得到如下输出结果:
合并后的数据:
-------------------------------
Person ID: 1, Person Name: 张三, AddressInfo: 北京市海淀区
Person ID: 2, Person Name: 李四, AddressInfo: 上海市浦东新区
Person ID: 3, Person Name: 王五, AddressInfo: 广州市天河区