using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); Console.WriteLine("Numbers in first array but not second array:"); foreach (var n in aOnlyNumbers) { Console.WriteLine(n); } } }
LINQ
One array expect another array
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{ public static void Main(){ int[] numbers = {1, 2, 3, 4}; int[] numbers2 = {1, 1, 3, 3}; Console.Write(numbers.Except(numbers2)); } }
Use Equal determines if two string arrays have the same elements in the same order
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { var wordsA = new string[] { "c", "ap", "b" }; var wordsB = new string[] { "c", "ap", "b" }; bool match = wordsA.Equals(wordsB); Console.WriteLine("The sequences match: {0}", match); } }
Use static method syntax to call the query operators.
using System; using System.Collections.Generic; using System.Linq; public class MainClass { public static void Main() { string[] names = { "J", "P", "G", "Pa" }; IEnumerable<string> filtered = Enumerable.Where(names,n => n.Contains("a")); IEnumerable<string> sorted = Enumerable.OrderBy(filtered, n => n.Length); IEnumerable<string> finalQuery = Enumerable.Select(sorted,n => n.ToUpper()); } }
Returning a Sequence of Ten Integers All With the Value Two
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { IEnumerable<int> ints = Enumerable.Repeat(2, 10); foreach (int i in ints) Console.WriteLine(i); } }
uses Repeat to generate a sequence that contains the number 7 ten times.
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { var numbers = Enumerable.Repeat(7, 10); foreach (var n in numbers) { Console.WriteLine(n); } } }
Calling the Range Operator
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { IEnumerable<int> ints = Enumerable.Range(1, 10); foreach (int i in ints) Console.WriteLine(i); } }