linq操作符:串联操作符

串联是一个将两个集合连接在一起的过程。在Linq中,这个过程通过Concat操作符实现。Concat操作符用于连接两个集合,生成一个新的集合。来看看Concat操作符的定义:

1 public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

 从方法定义中可以看出:第二个参数为输入一个新的集合,与调用集合连接,生成并返回一个新的集合。

注意:

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

请看下面的例子:

定义Category类,其类定义如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SeriesOperation
 8 {
 9     public class Category
10     {
11         public int Id { get; set; }
12         public string CategoryName { get; set; }
13         public DateTime CreateTime { get; set; }
14     }
15 }

 然后在Main()方法中调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SeriesOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 初始化数据
14             List<Category> listCategory = new List<Category>()
15             {
16               new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)},
17               new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)},
18               new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
19               new Category(){ Id=4,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)}
20             };
21 
22             List<Category> list = new List<Category>()
23             {
24               new Category(){ Id=5,CategoryName="管理类",CreateTime=DateTime.Now.AddDays(-34)},
25               new Category(){ Id=6,CategoryName="金融学",CreateTime=DateTime.Now.AddYears(-4)}
26             };
27 
28             // 查询表达式
29             var newListExpress = (from c in listCategory select c).Concat(from p in list select p);
30             Console.WriteLine("查询表达式输出:");
31             foreach (var item in newListExpress)
32             {
33                 Console.WriteLine($"Id:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}");
34             }
35 
36             var newList = listCategory.Concat(list);
37             Console.WriteLine("方法语法输出:");
38             foreach (var item in newList)
39             {
40                 Console.WriteLine($"Id:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}");
41             }
42 
43             Console.ReadKey();
44         }
45     }
46 }

 结果:

如何输出不同集合中相同类型的元素呢?看下面的例子:

在定义Product类,其定义如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SeriesOperation
 8 {
 9     public class Product
10     {
11         public int Id { get; set; }
12         public int CategoryId { get; set; }
13         public string Name { get; set; }
14         public double Price { get; set; }
15         public DateTime CreateTime { get; set; }
16     }
17 }

 Category类中的CategoryName和Product类中的Name都是string类型的,在下面的例子中输出Category中的CategoryName和Product中的Name。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace SeriesOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 初始化数据
14             List<Category> listCategory = new List<Category>()
15             {
16               new Category(){ Id=1,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-1)},
17               new Category(){ Id=2,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-2)},
18               new Category(){ Id=3,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-34)},
19               new Category(){ Id=4,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-34)}
20             };
21             List<Product> listProduct = new List<Product>()
22             {
23                new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
24                new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
25                new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
26                new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
27                new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
28             };
29 
30             // 查询表达式
31             var newList = (from p in listProduct
32                            select p.Name).Concat(from c in listCategory select c.CategoryName);
33             Console.WriteLine("查询表达式输出:");
34             foreach (var item in newList)
35             {
36                 Console.WriteLine(item);
37             }
38             Console.WriteLine("*************************");
39             // 方法语法
40             var newListFun = listProduct.Select(p => p.Name).Concat(listCategory.Select(c => c.CategoryName));
41             Console.WriteLine("方法语法输出:");
42             foreach (var item in newListFun)
43             {
44                 Console.WriteLine(item);
45             }
46 
47             Console.ReadKey();
48         }
49     }
50 }

 结果:

 

原文地址:https://www.cnblogs.com/dotnet261010/p/9311015.html