LINQ系列:Linq to Object集合操作符

集合操作符对元素的集合或序列集合进行操作,并返回一个集合。LINQ共有4种集合查询操作符:Distinct、Union、Intersect和Except。

1. Distinct

  Distinct操作符删除集合中重复的值,并返回该集合中互不相同的元素。

1>. 原型定义

public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source);
public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer);

2>. 示例

int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = from f in fibonacci
           select f;
expr.Distinct();
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = from f in fibonacci
           where f > 5
           select f;
expr.Distinct();
var expr = from p in context.Products
           select p.ProductName;
expr.Distinct();
var expr = context.Products
    .Select(p => p.ProductName)
    .Distinct();
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = fibonacci.Distinct().Count();
var expr = context.Products
    .Select(c => c.CategoryID)
    .Distinct()
    .Count();

 

int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = fibonacci.Count(f => f % 2 == 0);

 

2. Union

  Union操作符返回两个序列或集合并集中的每个互不相同的元素。与Concat操作符不同,Union操作符返回互不相同的元素,而Concat操作符将返回所有的值。

1>. 原型定义

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

2>. 示例

复制代码
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
int[] factorial = new int[] { 1, 2, 6, 24, 120 };

IEnumerable<int> expr = fibonacci.Union(factorial);

foreach (int item in expr)
{
    Console.Write(item + " ");
}
复制代码
1 2 3 5 8 13 21 6 24 120

3. Intersect

  Intersect操作符返回两个序列的交集,即返回那些同时存在于两个序列或集合中的值。

1>. 原型定义

public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

2>. 示例

复制代码
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
int[] factorial = new int[] { 1, 2, 6, 24, 120 };

IEnumerable<int> expr = fibonacci.Intersect(factorial);

foreach (int item in expr)
{
    Console.Write(item + " ");
}
复制代码
1 2

4. Except

  Except操作符与Intersect操作符相反,它返回两个序列中不同的部分,返回序列中所有不重复的值。

1>. 原型定义

public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer);

2>. 示例

复制代码
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
int[] factorial = new int[] { 1, 2, 6, 24, 120 };

IEnumerable<int> expr = fibonacci.Except(factorial);

foreach (int item in expr)
{
    Console.Write(item + " ");
}
复制代码
3 5 8 13 21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍狼_2001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值