LINQ series: LINQ to object set Operators

Source: Internet
Author: User
Tags prototype definition

The set operator operates on the set or sequence set of elements and returns a set. There are four collection query operators in LINQ: distinct, union, intersect, and except T.

1. Distinct

The distinct operator deletes duplicate values in a set and returns distinct elements in the set.

1>. Prototype Definition

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

2>. Example

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();

2. Union

The Union operator returns each element in two sequences or collections. Unlike the Concat operator, the Union operator returns distinct elements, while the Concat operator returns all values.

 

LINQ series: LINQ to object set Operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.