LINQ中连接操作符(九) --两个条件
多条件的情况就是把单个字段改写成匿名类的形式进行等值连接,这里只是列举内连接形式,左/右连接类似,只是在写条件串联的时候需要注意一下
SQL语句写法:
-- SQL语句的写法 select c.Id,c.CategoryName,p.Name "ProductName", p.CreateTime "PublishTime" from listCategory c,listProduct p where c.Id = p.Id and c.CategoryId = p.CategoryId
Sample Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConnectOperation { public class Category { public int Id { get; set; } public int CategoryId { get; set; } public string CategoryName { get; set; } public DateTime CreateTime { get; set; } } public class Product { public int Id { get; set; } public int CategoryId { get; set; } public string Name { get; set; } public double Price { get; set; } public DateTime CreateTime { get; set; } } class Program { static void Main(string[] args) { // 初始化数据 List<Category> listCategory = new List<Category>() { new Category(){ Id=1,CategoryId=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)}, new Category(){ Id=2,CategoryId=1,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)}, new Category(){ Id=3,CategoryId=2,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)}, new Category(){ Id=4,CategoryId=3,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)} }; List<Product> listProduct = new List<Product>() { new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=3, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=2, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 1、查询表达式 var queryExpress1 = from c in listCategory join p in listProduct on new { c.Id, c.CategoryId } equals new { p.Id, p.CategoryId } select new { Id = c.Id, CategoryName = c.CategoryName, ProductName = p.Name, PublishTime = p.CreateTime }; Console.WriteLine("查询表达式输出写法一:"); foreach (var item in queryExpress1) { Console.WriteLine($"id:{item.Id},CategoryName:{item.CategoryName},ProductName:{item.ProductName},PublishTime:{item.PublishTime}"); } var queryExpress2 = from c in listCategory from p in listProduct where c.Id.Equals(p.Id) && c.CategoryId.Equals(p.CategoryId) select new { Id = c.Id, CategoryName = c.CategoryName, ProductName = p.Name, PublishTime = p.CreateTime }; Console.WriteLine("查询表达式输出写法二:"); foreach (var item in queryExpress2) { Console.WriteLine($"id:{item.Id},CategoryName:{item.CategoryName},ProductName:{item.ProductName},PublishTime:{item.PublishTime}"); } // 2、方法语法 Console.WriteLine("方法语法输出:"); var queryFun = listCategory.Join(listProduct, c => new { c.Id, c.CategoryId }, p => new { p.Id, p.CategoryId }, (c, p) => new { Id = c.Id, CategoryName = c.CategoryName, ProductName = p.Name, PublishTime = p.CreateTime }); foreach (var item in queryFun) { Console.WriteLine($"id:{item.Id},CategoryName:{item.CategoryName},ProductName:{item.ProductName},PublishTime:{item.PublishTime}"); } Console.ReadKey(); } } }