1、Concat操作符
Concat
操作符用于连接两个序列,生成一个新序列。
例如,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<People> pList = new List<People>();
People p1 = new People(1, "C", 4);
People p2 = new People(2, "Java", 7);
People p3 = new People(3, "Python", 11);
People p4 = new People(4, "Linux", 15);
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);
pList.Add(p4);
List<People> pList1 = new List<People>();
People p5 = new People(5,"CJavaPY",16);
pList1.Add(p5);
IEnumerable<People> newList = pList.Concat(pList1);
foreach (var item in newList)
{
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
}
public class People
{
public People(int id, string name, int age)
{
this.Id = id;
this.Name = name;
this.Age = age;
}
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
}
2、Union操作符
Union
操作符用于将两个序列中的元素合并成一个新的序列,新序列将自动去除重复的元素。
例如,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<int> listInt = new List<int>();
listInt.Add(1);
listInt.Add(2);
listInt.Add(3);
List<int> listInt1 = new List<int>();
listInt1.Add(2);
listInt1.Add(3);
listInt1.Add(4);
//Concat的使用
IEnumerable<int> IEInt = listInt.Concat(listInt1);
foreach (var i in IEInt)
{
Console.WriteLine(i);
}
Console.WriteLine();
//Union的使用
IEnumerable<int> IEInt1 = listInt.Union(listInt1);
foreach (var i in IEInt1)
{
Console.WriteLine(i);
}
//根据输出结果可以看出Concat和Union的区别
Console.ReadKey();
}
}
}