串联是一个将两个集合连接在一起的过程。在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. WCF 有零个操作;协定必须至少有一个操作

    转自 http://www.cnblogs.com/bdqlaccp/archive/2011/12/31/2308905.html 建立WCF服务后, 服务类中写上了相应的操作,并且方法上加上了[O ...

  2. 【Android API】Android 4.1 API官方文档详解

    原文:http://android.eoe.cn/topic/summary 翻译:[eoeAndroid原创团队]kris.流风而逝.贼寇在何方.snowxwyo.lsy4833406 更新日期:2 ...

  3. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  4. 分享:三种取消选中单选框radio的方法

    三种取消选中radio的方式,本文依赖于jQuery,其中第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DOM实现的. <!DOCTYPE HTML> <html ...

  5. jqgrid 设置为每行单选

    jqgrid 不支持单选,自己自带了多选multiselect 那么单选怎么做呢,可以参考如下配置 multiselect: true, multiboxonly:true, gridComplete ...

  6. mydqldump 备份数单库 然后还原数据的时候报:ERROR 1881 (HY000) at line 52: Operation not allowed when innodb_forced_recovery > 0.

    修改my.cnf innodb_force_recovery = 1 修改为: innodb_force_recovery = 0

  7. Redis数据类型应用场景及具体方法总结

    StringsStrings 数据结构是简单的key-value类型,value其实不仅是String,也可以是数字.使用Strings类型,你可以完全实现目前 Memcached 的功能,并且效率更 ...

  8. nil

    Lua中特殊的类型,他只有一个值:nil:一个全局变量没有被赋值以前默认值为nil:给全局变量负nil可以删除该变量.

  9. blocking

    package blockingtest; /** * Hello world! */public class Sync implements Runnable { int n; public sta ...

  10. Swift中Notification.Name这么难用怎么办

    Swift中Notification.Name这么难用怎么办 以前的发送通知的参数就是一个简单的字符串: NSNotificationCenter.defaultCenter().post(" ...