串联是一个将两个集合连接在一起的过程。在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=,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-)},
new Category(){ Id=,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-)}
}; List<Category> list = new List<Category>()
{
new Category(){ Id=,CategoryName="管理类",CreateTime=DateTime.Now.AddDays(-)},
new Category(){ Id=,CategoryName="金融学",CreateTime=DateTime.Now.AddYears(-)}
}; // 查询表达式
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=,CategoryName="计算机",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="文学",CreateTime=DateTime.Now.AddYears(-)},
new Category(){ Id=,CategoryName="高校教材",CreateTime=DateTime.Now.AddMonths(-)},
new Category(){ Id=,CategoryName="心理学",CreateTime=DateTime.Now.AddMonths(-)}
};
List<Product> listProduct = new List<Product>()
{
new Product(){Id=,CategoryId=, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
new Product(){Id=,CategoryId=, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-)},
new Product(){Id=,CategoryId=, Name="活着", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="高等数学", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-)}
}; // 查询表达式
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();
}
}
}

结果:

linq操作符:串联操作符的更多相关文章

  1. LINQ标准查询操作符详解(转)

     一. 关于LINQ       LINQ 英文全称是“Language-Integrated Query”,中文为“语言集成查询”,它是微软首席架构师.Delphi 之父和C# 之父——Anders ...

  2. LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、

    四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...

  3. Linq 标准查询操作符三

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  4. LINQ 标准查询操作符

    本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...

  5. Linq学习之操作符

    一.环境搭建 下面将逐步搭建我们学习的环境,这个环境不仅仅是这次需要使用,以后的教程一样需要使用这个环境.所以请大家务必按照 搭建这里的环境否则会影响你后面的学习. 我们用到的几张表 通知消息表: 用 ...

  6. LINQ标准查询操作符(三)——Aggregate、Average、Distinct、Except、Intersect、Union、Empty、DefaultIfEmpty、Range、Repeat

    七.聚合操作符 聚合函数将在序列上执行特定的计算,并返回单个值,如计算给定序列平均值.最大值等.共有7种LINQ聚合查询操作符:Aggregate.Average.Count.LongCount.Ma ...

  7. Linq标准查询操作符

     Linq的出现让代码简洁了不少.之前在项目中基本都在使用它,但是没有完整的整理过,今天借这个周末,将其进行整理,方便后期对其的使用.Linq的操作可以分为聚合,连接,转换,元素操作符,相等操作,生成 ...

  8. LINQ标准查询操作符(四) —AsEnumerable,Cast,OfType,ToArray,ToDictionary,ToList,ToLookup,First,Last,ElementAt

    十.转换操作符 转换操作符是用来实现将输入对象的类型转变为序列的功能.名称以“As”开头的转换方法可更改源集合的静态类型但不枚举(延迟加载)此源集合.名称以“To”开头的方法可枚举(即时加载)源集合并 ...

  9. LINQ标准查询操作符(一)——select、SelectMany、Where、OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse

    一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: //查询语法 var query = fro ...

  10. Kotlin——最详细的操作符与操作符重载详解(上)

    本篇文章为大家详细的介绍Koltin特有的操作符重载.或许对于有编程经验的朋友来说,操作符这个词绝对不陌生,就算没有任何编辑基础的朋友,数学中的算数运算符也绝不陌生.例如(+.-.*./.>.& ...

随机推荐

  1. ubuntu 安装python mysqldb

    sudo apt-get install python-mysqldb #!/usr/bin/python #-*-coding:utf-8-*- ''' This file include all ...

  2. elk 使用中遇到的问题(kafka 重复消费)

    问题描述: 在使用过程中,当遇到大量报错的时候,我们到eagle后台看到报错的那个consumer的消费情况到到lag 远远大于0(正常情况应该为0),activie  节点没有,kibana面板上没 ...

  3. Android 加载大图

    在 Android 开发中, Bitmap 是个吃内存大户,稍微操作不当就会 OOM .虽然现在第三方的图片加载库已经很多,很完善,但是作为一个 Androider 还得知道如何自己进行操作来加载大图 ...

  4. Flink PPT

    杭州第六次 Spark & Flink Meetup 资料分享 https://github.com/397090770/Spark-Flink-Meetup-6-Hangzhou https ...

  5. redhat7.2 安装docker

    # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # sed -i  ' ...

  6. Vue.js使用-组件示例(实现数据的CRUD)

    1.业务场景 用户(姓名,年龄,性别)的增删改查 2.数据格式 定义字段,name:字段名称,iskey:是否主键(添加,修改数据标志),dataSource:可选列表(下拉框选项) columns: ...

  7. linux c编程操作数据库(sqlite3应用)

     首先pThread 不是linux系统默认库,连接的时候需要使用库libpthread.a. 加入-lpthread参数.另外会有lopen什么找不到的情况.加入-ldl 指定目录.Project_ ...

  8. node的http请求

    //node的http服务 'use strict' var http = require('http') var server = http.createServer(function (reque ...

  9. java连接https时禁用证书验证.

    import java.io.File; import java.security.cert.CertificateException; import java.util.List; import j ...

  10. 【转】MATLAB conv2函数的理解

    另附:http://blog.csdn.net/anan1205/article/details/12313593 原文:http://blog.csdn.net/andrewseu/article/ ...