Join 子句有三种最常见的联接类型:内部联接,分组联接,左外部联接
本次介绍:内部联接
按照关系数据库的说法,“内部联接”产生一个结果集,对于该结果集内第一个集合中的每个元素,
只要在第二个集合中存在一个匹配元素,该元素就会出现一次。
如果第一个集合中的某个元素没有匹配元素,则它不会出现在结果集内。
Code Demo:
1.Single Join
2. Compound Join
3.Multiple Join
//
Single Join
// 将 A 对象与其 b2 为该 A 的 B 对象进行匹配。
private void SingleJoin()
{
Data#region Data
A magnus = new A { a1 = "Magnus", a2 = "Hedlund" };
A terry = new A { a1 = "Terry", a2 = "Adams" };
B barley = new B { b1 = "Barley", b2 = terry };
B boots = new B { b1 = "Boots", b2 = terry };
List<A> aa = new List<A> { magnus, terry };
List<B> bb = new List<B> { barley, boots };
#endregion
var query = from a in aa
join b in bb
on a equals b.b2
select new { selectA = a.a1, selectB = b.b1 };
foreach (var ab in query)
{
Console.WriteLine("{0} and {1}", ab.selectA, ab.selectB);
}
}
// 将 A 对象与其 b2 为该 A 的 B 对象进行匹配。
private void SingleJoin()
{
Data#region Data
A magnus = new A { a1 = "Magnus", a2 = "Hedlund" };
A terry = new A { a1 = "Terry", a2 = "Adams" };
B barley = new B { b1 = "Barley", b2 = terry };
B boots = new B { b1 = "Boots", b2 = terry };
List<A> aa = new List<A> { magnus, terry };
List<B> bb = new List<B> { barley, boots };
#endregion
var query = from a in aa
join b in bb
on a equals b.b2
select new { selectA = a.a1, selectB = b.b1 };
foreach (var ab in query)
{
Console.WriteLine("{0} and {1}", ab.selectA, ab.selectB);
}
}
//
使用复合键可基于多个属性来比较元素
private void CompoundJoin()
{
Data#region Data
// Create a list of employees.
List<Employee> employees = new List<Employee> {
new Employee { FirstName = "Terry", LastName = "Adams", EmployeeID = 522459 },
new Employee { FirstName = "Charlotte", LastName = "Weiss", EmployeeID = 204467 },
new Employee { FirstName = "Magnus", LastName = "Hedland", EmployeeID = 866200 },
new Employee { FirstName = "Vernette", LastName = "Price", EmployeeID = 437139 } };
// Create a list of students.
List<Developer> devs = new List<Developer> {
new Developer { FirstName = "Vernette", LastName = "Price", DeveloperID = 9562 },
new Developer { FirstName = "Terry", LastName = "Earls", DeveloperID = 9870 },
new Developer { FirstName = "Terry", LastName = "Adams", DeveloperID = 9913 } };
#endregion
/**////联接两个数据源,查找既是员工又是学生的记录
IEnumerable<String> query = from employee in employees
join dev in devs
on new { employee.FirstName, employee.LastName }
equals new { dev.FirstName, dev.LastName }
select employee.FirstName + " && " + employee.LastName;
foreach (string name in query)
Console.WriteLine(name);
}
private void CompoundJoin()
{
Data#region Data
// Create a list of employees.
List<Employee> employees = new List<Employee> {
new Employee { FirstName = "Terry", LastName = "Adams", EmployeeID = 522459 },
new Employee { FirstName = "Charlotte", LastName = "Weiss", EmployeeID = 204467 },
new Employee { FirstName = "Magnus", LastName = "Hedland", EmployeeID = 866200 },
new Employee { FirstName = "Vernette", LastName = "Price", EmployeeID = 437139 } };
// Create a list of students.
List<Developer> devs = new List<Developer> {
new Developer { FirstName = "Vernette", LastName = "Price", DeveloperID = 9562 },
new Developer { FirstName = "Terry", LastName = "Earls", DeveloperID = 9870 },
new Developer { FirstName = "Terry", LastName = "Adams", DeveloperID = 9913 } };
#endregion
/**////联接两个数据源,查找既是员工又是学生的记录
IEnumerable<String> query = from employee in employees
join dev in devs
on new { employee.FirstName, employee.LastName }
equals new { dev.FirstName, dev.LastName }
select employee.FirstName + " && " + employee.LastName;
foreach (string name in query)
Console.WriteLine(name);
}
//
可以将任意数量的联接操作拼接在一起以执行多联接。
private void MultipleJoin()
{
Data#region Data
Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
Person rui = new Person { FirstName = "Rui", LastName = "Raposo" };
Person phyllis = new Person { FirstName = "Phyllis", LastName = "Harris" };
Cat barley = new Cat { Name = "Barley", Owner = terry };
Cat boots = new Cat { Name = "Boots", Owner = terry };
Cat whiskers = new Cat { Name = "Whiskers", Owner = charlotte };
Cat bluemoon = new Cat { Name = "Blue Moon", Owner = rui };
Cat daisy = new Cat { Name = "Daisy", Owner = magnus };
Dog fourwheeldrive = new Dog { Name = "Four Wheel Drive", Owner = phyllis };
Dog duke = new Dog { Name = "Duke", Owner = magnus };
Dog denim = new Dog { Name = "Denim", Owner = terry };
Dog wiley = new Dog { Name = "Wiley", Owner = charlotte };
Dog snoopy = new Dog { Name = "Snoopy", Owner = rui };
Dog snickers = new Dog { Name = "Snickers", Owner = arlene };
// Create three lists.
List<Person> people =
new List<Person> { magnus, terry, charlotte, arlene, rui, phyllis };
List<Cat> cats =
new List<Cat> { barley, boots, whiskers, bluemoon, daisy };
List<Dog> dogs =
new List<Dog> { fourwheeldrive, duke, denim, wiley, snoopy, snickers };
#endregion
//从多联接查询可以看出来,就是一个多表join查询,呵呵
//第一个join与 Cat.Owner 匹配的Person 对象将Master与Cat进行匹配,
// 并返回包含 Person 对象和 Cat.Name 的匿名类型的结果。
//第二个join是拿“Person 对象和 Cat.Name 的匿名类型的序列”与Dog相关联,
// 而该复合键由 Person 类型的 Owner 属性和动物名字的首字母组成。
//该子句返回一个匿名类型序列,这些类型包含每个匹配对中的 Cat.Name 和 Dog.Name 属性。]
//由于这是一个内部联接,因此仅返回第一个数据源中那些在第二个数据源中具有匹配对象的对象。
//person相同的,catName与dogName首字母相同的结果。即只要两条.
var query = from person in people
join cat in cats
on person
equals cat.Owner
join dog in dogs
on new { Owner = person, Letter = cat.Name.Substring(0, 1) }
equals new { dog.Owner, Letter = dog.Name.Substring(0, 1) }
select new { CatName = cat.Name, DogName = dog.Name };
foreach (var obj in query)
{
Console.WriteLine("{0} && {1}", obj.CatName, obj.DogName);
}
}
private void MultipleJoin()
{
Data#region Data
Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
Person rui = new Person { FirstName = "Rui", LastName = "Raposo" };
Person phyllis = new Person { FirstName = "Phyllis", LastName = "Harris" };
Cat barley = new Cat { Name = "Barley", Owner = terry };
Cat boots = new Cat { Name = "Boots", Owner = terry };
Cat whiskers = new Cat { Name = "Whiskers", Owner = charlotte };
Cat bluemoon = new Cat { Name = "Blue Moon", Owner = rui };
Cat daisy = new Cat { Name = "Daisy", Owner = magnus };
Dog fourwheeldrive = new Dog { Name = "Four Wheel Drive", Owner = phyllis };
Dog duke = new Dog { Name = "Duke", Owner = magnus };
Dog denim = new Dog { Name = "Denim", Owner = terry };
Dog wiley = new Dog { Name = "Wiley", Owner = charlotte };
Dog snoopy = new Dog { Name = "Snoopy", Owner = rui };
Dog snickers = new Dog { Name = "Snickers", Owner = arlene };
// Create three lists.
List<Person> people =
new List<Person> { magnus, terry, charlotte, arlene, rui, phyllis };
List<Cat> cats =
new List<Cat> { barley, boots, whiskers, bluemoon, daisy };
List<Dog> dogs =
new List<Dog> { fourwheeldrive, duke, denim, wiley, snoopy, snickers };
#endregion
//从多联接查询可以看出来,就是一个多表join查询,呵呵
//第一个join与 Cat.Owner 匹配的Person 对象将Master与Cat进行匹配,
// 并返回包含 Person 对象和 Cat.Name 的匿名类型的结果。
//第二个join是拿“Person 对象和 Cat.Name 的匿名类型的序列”与Dog相关联,
// 而该复合键由 Person 类型的 Owner 属性和动物名字的首字母组成。
//该子句返回一个匿名类型序列,这些类型包含每个匹配对中的 Cat.Name 和 Dog.Name 属性。]
//由于这是一个内部联接,因此仅返回第一个数据源中那些在第二个数据源中具有匹配对象的对象。
//person相同的,catName与dogName首字母相同的结果。即只要两条.
var query = from person in people
join cat in cats
on person
equals cat.Owner
join dog in dogs
on new { Owner = person, Letter = cat.Name.Substring(0, 1) }
equals new { dog.Owner, Letter = dog.Name.Substring(0, 1) }
select new { CatName = cat.Name, DogName = dog.Name };
foreach (var obj in query)
{
Console.WriteLine("{0} && {1}", obj.CatName, obj.DogName);
}
}