-
Linq Coding Part Five (Join之内部联接查询)
Join:其实就是可以将来自不同源序列并且在对象模型中没有直接关系的元素相关联。
Join 子句有三种最常见的联接类型:内部联接,分组联接,左外部联接
本次介绍:内部联接
按照关系数据库的说法,“内部联接”产生一个结果集,对于该结果集内第一个集合中的每个元素,
只要在第二个集合中存在一个匹配元素,该元素就会出现一次。
如果第一个集合中的某个元素没有匹配元素,则它不会出现在结果集内。
Code Demo:
1.Single Join
2.Compound Join
3.Multiple Join
//Single Join
//将 A 对象与其 b2 为该 A 的 B 对象进行匹配。
private void SingleJoin()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Data#region Data
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
A magnus = new A
{ a1 = "Magnus", a2 = "Hedlund" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
A terry = new A
{ a1 = "Terry", a2 = "Adams" };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
B barley = new B
{ b1 = "Barley", b2 = terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
B boots = new B
{ b1 = "Boots", b2 = terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<A> aa = new List<A>
{ magnus, terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<B> bb = new List<B>
{ barley, boots };
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var query = from a in aa
join b in bb
on a equals b.b2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{ selectA = a.a1, selectB = b.b1 };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var ab in query)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} and {1}", ab.selectA, ab.selectB);
}
}
//使用复合键可基于多个属性来比较元素
private void CompoundJoin()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Data#region Data
// Create a list of employees.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<Employee> employees = new List<Employee>
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Employee
{ FirstName = "Terry", LastName = "Adams", EmployeeID = 522459 },
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Employee
{ FirstName = "Charlotte", LastName = "Weiss", EmployeeID = 204467 },
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Employee
{ FirstName = "Magnus", LastName = "Hedland", EmployeeID = 866200 },
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Employee
{ FirstName = "Vernette", LastName = "Price", EmployeeID = 437139 } };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create a list of students.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
List<Developer> devs = new List<Developer>
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Developer
{ FirstName = "Vernette", LastName = "Price", DeveloperID = 9562 },
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Developer
{ FirstName = "Terry", LastName = "Earls", DeveloperID = 9870 },
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new Developer
{ FirstName = "Terry", LastName = "Adams", DeveloperID = 9913 } };
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////联接两个数据源,查找既是员工又是学生的记录
IEnumerable<String> query = from employee in employees
join dev in devs
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
on new
{ employee.FirstName, employee.LastName }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
equals new
{ dev.FirstName, dev.LastName }
select employee.FirstName + " && " + employee.LastName;
foreach (string name in query)
Console.WriteLine(name);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
//可以将任意数量的联接操作拼接在一起以执行多联接。
private void MultipleJoin()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Data#region Data
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person magnus = new Person
{ FirstName = "Magnus", LastName = "Hedlund" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person terry = new Person
{ FirstName = "Terry", LastName = "Adams" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person charlotte = new Person
{ FirstName = "Charlotte", LastName = "Weiss" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person arlene = new Person
{ FirstName = "Arlene", LastName = "Huff" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person rui = new Person
{ FirstName = "Rui", LastName = "Raposo" };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Person phyllis = new Person
{ FirstName = "Phyllis", LastName = "Harris" };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cat barley = new Cat
{ Name = "Barley", Owner = terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cat boots = new Cat
{ Name = "Boots", Owner = terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cat whiskers = new Cat
{ Name = "Whiskers", Owner = charlotte };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cat bluemoon = new Cat
{ Name = "Blue Moon", Owner = rui };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Cat daisy = new Cat
{ Name = "Daisy", Owner = magnus };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog fourwheeldrive = new Dog
{ Name = "Four Wheel Drive", Owner = phyllis };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog duke = new Dog
{ Name = "Duke", Owner = magnus };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog denim = new Dog
{ Name = "Denim", Owner = terry };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog wiley = new Dog
{ Name = "Wiley", Owner = charlotte };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog snoopy = new Dog
{ Name = "Snoopy", Owner = rui };
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Dog snickers = new Dog
{ Name = "Snickers", Owner = arlene };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Create three lists.
List<Person> people =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new List<Person>
{ magnus, terry, charlotte, arlene, rui, phyllis };
List<Cat> cats =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new List<Cat>
{ barley, boots, whiskers, bluemoon, daisy };
List<Dog> dogs =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new List<Dog>
{ fourwheeldrive, duke, denim, wiley, snoopy, snickers };
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从多联接查询可以看出来,就是一个多表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
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
on new
{ Owner = person, Letter = cat.Name.Substring(0, 1) }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
equals new
{ dog.Owner, Letter = dog.Name.Substring(0, 1) }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{ CatName = cat.Name, DogName = dog.Name };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (var obj in query)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("{0} && {1}", obj.CatName, obj.DogName);
}
}
LINQ Coding 目录![](https://www.cnblogs.com/Emoticons/baimantou/223332344.gif)
- Linq Coding -- Part One
- Linq Coding -- Part Two[标准查询运算符]
- Linq Coding -- Part Three [Let子句]
- Linq Coding -- Part Four[Concat应用]
- Linq Coding -- Part Five (Join之内部联接查询)
- Linq Coding -- Part Six (Join之分组联接)
- Linq Coding -- Part Seven (Join之左外部联接、DefaultIfEmpty、GroupJoin)
-
相关阅读:
Delphi xe8 FMX StringGrid根据内容自适应列宽。
Delphi 10.3.1 Secure File Sharing解决应用间文件共享
分享一个求时间差大于多少秒的函数
解决android 9上无法使用http协议
【转】FMX 动态创建及销毁(释放free)对象
ChinaCock界面控件介绍-TCCBarcodeCreator
IDE Fix Pack 6.4.4 released (bugfix release)
Android & iOS 启动画面工具
REST easy with kbmMW #24 使用kbmMW实现JSON/XML/YAML转换成对象
关于ElasticSearch的聚类时出现fielddata=true问题
-
原文地址:https://www.cnblogs.com/RuiLei/p/1089896.html
Copyright © 2020-2023
润新知