linq中的串联操作符

这篇文章介绍了linq中的串联操作符,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

public static IEnumerable Concat(this IEnumerable first, IEnumerable second)

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

注意:

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

请看下面的例子:

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { public class Category { public int Id { get; set; } public string CategoryName { get; set; } public DateTime CreateTime { get; set; } } }

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { class Program { static void Main(string[] args) { // 初始化数据 List listCategory = new List() { 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 list = new List() { 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:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}"); } var newList = listCategory.Concat(list); Console.WriteLine("方法语法输出:"); foreach (var item in newList) { Console.WriteLine($"Id:{item.Id},CategoryName:{item.CategoryName},CreateTime:{item.CreateTime}"); } Console.ReadKey(); } } }

结果:

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

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { 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; } } }

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

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SeriesOperation { class Program { static void Main(string[] args) { // 初始化数据 List listCategory = new List() { 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 listProduct = new List() { 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(); } } }

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是linq中的串联操作符的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程