LINQ 查询表达式join 关键字用法:
我在这里先创建的实验用例:
class Customer { public int CustomerId { get; set; } public string Name { get; set; } public int Age { get; set; } } class Product { public int ProductId { get; set; } public string Name { get; set; } public string Origin { get; set; } } class Order { public int OrderId { get; set; } public int CustomerId { get; set; } public List<Product> Products { get; set; } } static List<Customer> customers; static List<Product> products; static List<Order> orders; public static void CreateEntities() { customers = new List<Customer>{ new Customer(){ CustomerId = 1, Name = "CA", Age=13}, new Customer(){ CustomerId = 2, Name = "CB", Age=13}, new Customer(){ CustomerId = 3, Name = "CC", Age=13}, new Customer(){ CustomerId = 4, Name = "CD", Age=13} }; products = new List<Product>{ new Product(){ ProductId = 1, Name = "PA", Origin="P1" }, new Product(){ ProductId = 2, Name = "PB", Origin="P2" }, new Product(){ ProductId = 3, Name = "PC", Origin="P1" }, new Product(){ ProductId = 4, Name = "PD", Origin="P3" } }; orders = new List<Order>{ new Order(){ OrderId = 1 , CustomerId =1, Products = new List<Product>{ new Product(){ ProductId = 2, Name = "PB", Origin="P2" }, new Product(){ ProductId = 3, Name = "PC", Origin="P1" } }}, new Order(){ OrderId = 2 , CustomerId =1, Products = new List<Product>{ new Product(){ ProductId = 3, Name = "PC", Origin="P1" }, new Product(){ ProductId = 4, Name = "PD", Origin="P3" } }}, new Order(){ OrderId = 3 , CustomerId =3, Products = new List<Product>{ new Product(){ ProductId = 4, Name = "PD", Origin="P3" } }}, new Order(){ OrderId = 4 , CustomerId =2, Products = new List<Product>{ new Product(){ ProductId = 1, Name = "PA", Origin="P1" }, new Product(){ ProductId = 4, Name = "PD", Origin="P3" } }} }; }
我们在做SQL查询的时候经常会用到Inner Join,Left Join,笛卡尔积等等,连接方式的概念方面我想也不用给予太多解释,
我们今天的重点是让大家熟悉LINQ是如何使用Join来实现常用的表连接的。
我们用以下例子来熟悉 Join 关键字的用法。
1.Inner Join:
CreateEntities(); var query = from c in customers join o in orders on c.CustomerId equals o.CustomerId where o.OrderId == 2 select c; foreach (var customer in query) { Console.WriteLine("Id:{0}, Name:{1}", customer.CustomerId, customer.Name); }
运行结果:
Id:1, Name:CA
上面这个是常见的内连接的例子,和SQL语法也很相似,但有以下几点要注意:
(1).连接条件: c.CustomerId equals o.CustomerId 只能使用 equals 不能用 =,==,等于 等表示。
以为LINQ的设计者认为 几乎所有的连接条件都是 = 条件不会出现 >,<,!= 等情况因此使用了个关键字来描述表连接条件。
(2).条件顺序:c.CustomerId equals o.CustomerId ,range variable: c 和b之前的顺序不能颠倒。
2.Group Join:
也许大家对Group Join的概念不太了解,没关系让我们通过例子来认识它:
CreateEntities(); var query = from c in customers join o in orders on c.CustomerId equals o.CustomerId into os select new { c, os }; foreach (var item in query) { Console.WriteLine("Customer Id:{0}, Name:{1}", item.c.CustomerId, item.c.Name); foreach (var o in item.os) { Console.WriteLine("--Order Id:{0}", o.OrderId); } }
结果:
Customer Id:1, Name:CA
--Order Id:1
--Order Id:2
Customer Id:2, Name:CB
--Order Id:4
Customer Id:3, Name:CC
--Order Id:3
Customer Id:4, Name:CD
Press any key to continue . . .
以上查询返回的结果:和Group By 返回的结果非常的相似:一个KEY对象对应一个集合。
要实现Group Join我们要引入一个关键字:into
但使用时要注意一下几点:
(1).使用into 关键字后 join 后面的 range variable:o 在后面的表达式块中就失去了作用域。
(2).range variable:os 通常情况下都是IEnumerable<T>类型的。
3.Left Join:
Left Join 我们在SQL里经常用到,让我们来看看LINQ里怎么实现它:
CreateEntities(); var query = from c in customers join o in orders on c.CustomerId equals o.CustomerId into os from o2 in os.DefaultIfEmpty( new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() }) select new { c, o2 }; foreach (var item in query) { Console.WriteLine("Customer Id:{0}, Name:{1}--Order Id:{0}", item.c.CustomerId, item.o2.OrderId); }
结果:
Customer Id:1, Name:1--Order Id:1
Customer Id:1, Name:2--Order Id:1
Customer Id:2, Name:4--Order Id:2
Customer Id:3, Name:3--Order Id:3
Customer Id:4, Name:0--Order Id:4
Press any key to continue . . .
我们可以看到Left Outer Join 的语法进一步的复杂化了,结果也有细微的不同。
(1).从语法上:
from o2 in os.DefaultIfEmpty(
new Order { OrderId = 0, CustomerId = 0, Products = new List<Product>() })
主要区别在于以上者1句语句。查询方法DefaultIfEmpty 用于定义当查询记录为空时,预定义默认值。再从其集合中取出子元素。
(2).从结果上:
我们在遍历查询结果时可以发现Left Join相似于Inner Join结果都是“平面”的,然而Group Join返回的结果具有层次性。
题外:
由于C#是面向对象的,往往会通过对象与对象间的外系来实现数据间关系。有时表达2个之间的关系也可以不使用Join关键字,
因此Join关键字其实在实际LINQ查询表达式中用的不是很多。
站内连接: