linq中的串联操作符

 更新时间:2022年03月10日 09:53:31   作者:.NET开发菜鸟  
这篇文章介绍了linq中的串联操作符,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> 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<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:{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<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();
        }
    }
}

结果:

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

相关文章

  • ASP.net做的IP访问限制

    ASP.net做的IP访问限制

    ASP.net做的IP访问限制...
    2006-09-09
  • C# 命名规则(挺不错的)

    C# 命名规则(挺不错的)

    我自己总结的一套命名规则,其实规则很重要,它是一种标准,可有可无,但有总会比无好,大家正在编码的同志仔细看看,给点改进意见。
    2009-02-02
  • 深入讲解.Net Core中的Api版本控制

    深入讲解.Net Core中的Api版本控制

    这篇文章主要给大家介绍了关于.Net Core中Api版本控制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • 通过ASP.net实现flash对数据库的访问

    通过ASP.net实现flash对数据库的访问

    近来网站需要在flash中提取数据库中的数据,从网上找了一点资料,今天下午在自己的机器上实现了一下,还是比较简单的。
    2009-08-08
  • 如何在ASP.Net Core中使用 IHostedService的方法

    如何在ASP.Net Core中使用 IHostedService的方法

    这篇文章主要介绍了如何在ASP.Net Core中使用 IHostedService的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 强烈推荐一个基于.Net Framework开发的Windows右键菜单管理工具

    强烈推荐一个基于.Net Framework开发的Windows右键菜单管理工具

    这篇文章主要介绍了推荐一个基于.Net Framework开发的Windows右键菜单管理工具,今天给大家推荐一个Windows右键菜单管理工具,方便我们管理我们的右键菜单,需要的朋友可以参考下
    2023-05-05
  • Json.net 常用使用小结(推荐)

    Json.net 常用使用小结(推荐)

    下面小编就为大家带来一篇Json.net 常用使用小结(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • ASP.NET实现上传Excel功能

    ASP.NET实现上传Excel功能

    本文主要介绍了ASP.NET 实现上传EXCEL,利用NOPI操作,转换得到DataTable的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-03-03
  • asp.net 图片的读写入库实现代码

    asp.net 图片的读写入库实现代码

    asp.net对图片的读写,实现将图片保存到数据库中,然后再读取显示的实现代码。
    2009-11-11
  • MVC生成页码选择器返回HTML代码详解

    MVC生成页码选择器返回HTML代码详解

    这篇文章主要为大家详细介绍了MVC生成页码选择器返回HTML代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论