Except

In this chapter you will learn:

  1. How to use Except operator
  2. Except with String array

Get to know Except operator

Except returns the elements in the first input sequence that are not present in the second:

using System;/*from  ja  va2s  .  c o  m*/
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] seq1 = { 1, 2, 3 };
        int[] seq2 = { 3, 4, 5 };

        IEnumerable<int> difference1 = seq1.Except(seq2);
        IEnumerable<int> difference2 = seq2.Except(seq1);

        foreach (int i in difference1)
        {
            Console.WriteLine(i);
        }
        foreach (int i in difference2)
        {
            Console.WriteLine(i);
        }
    }
}

The output:

Except with String array

using System;// ja v  a 2  s.  co  m
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


public class MainClass{

   public static void Main(string[] args){   
         String[] First = { "One", "Two", "Two", "Three", "Four" };
         String[] Second = { "Three", "Four", "Five", "Six" };

         var ShowExcept = First.Except(Second);

         Console.WriteLine("Except:");
         foreach (String ThisElement in ShowExcept)
            Console.WriteLine(ThisElement);

   }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use FindAll operator
Home » C# Tutorial » Linq Operators
Aggregate
Aggregate with seed
Aggregate string value
All
Any
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
FindAll
First
FirstOrDefault
GroupBy
Intersect
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Range
Repeat
Reverse
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipWhile
Sum
Take
TakeWhile
ThenBy
ThenByDescending
ToArray
ToList
Zip