LINQ中Cancat操作符(八)

串联是一个将两个集合连接在一起的过程。在Linq中,这个过程通过Concat操作符实现。Concat操作符用于连接两个集合,生成一个新的集合,类似于SQL语句中的Union操作

(第一个集合和第二个集合的元素的类型必须是相同的)

一、相同集合中的串联 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Category
    {
        public int Id { get; set; }
        public string CategoryName { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Category> listCategory = new List<Category>()
            {
              new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)},
              new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)},
              new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
              new Category(){ Id=4,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)}
            };

            List<Category> list = new List<Category>()
            {
              new Category(){ Id=5,CategoryName="管理类",CreateTime=DateTime.Now.AddDays(-34)},
              new Category(){ Id=6,CategoryName="金融学",CreateTime=DateTime.Now.AddYears(-4)}
            };

            // 查询表达式
            var newListExpress = (from c in listCategory select c).Concat(from p in list select p);
            Console.WriteLine("查询表达式输出:");
            foreach (var item in newListExpress)
            {
                Console.WriteLine("Id:{0},CategoryName:{1},CreateTime:{2}", item.Id, item.CategoryName, item.CreateTime);
            }

            // 查询方法
            var newList = listCategory.Concat(list);
            Console.WriteLine("方法语法输出:");
            foreach (var item in newList)
            {
                Console.WriteLine("Id:{0},CategoryName:{1},CreateTime:{2}", item.Id, item.CategoryName, item.CreateTime);
            }

            Console.ReadKey();
        }
    }
}
View Code

二、不同集合中的串联 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderOperation
{
    public class Category
    {
        public int Id { 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)
        {
            //Category类中的CategoryName和Product类中的Name都是string类型的,在下面的例子中输出Category中的CategoryName和Product中的Name。
            // 初始化数据
            List<Category> listCategory = new List<Category>()
            {
              new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)},
              new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)},
              new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
              new Category(){ Id=4,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=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
               new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
            };

            // 查询表达式
            var newList = (from p in listProduct
                           select p.Name).Concat(from c in listCategory select c.CategoryName);
            Console.WriteLine("查询表达式输出:");
            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("*************************");
            // 方法语法
            var newListFun = listProduct.Select(p => p.Name).Concat(listCategory.Select(c => c.CategoryName));
            Console.WriteLine("方法语法输出:");
            foreach (var item in newListFun)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}
View Code

posted @ 2020-04-14 14:34  LuckyZLi  阅读(185)  评论(0编辑  收藏  举报