Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0380 Concat and Union

Some query operators accept two input sequences. Concat appends one sequence to another Union does the same but with duplicates removed: using System; 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> concat = seq1.Concat(seq2); // { 1, 2, 3, 3, 4, 5 } IEnumerable<int> union = seq1.Union(seq2); // { 1, 2, 3, 4, 5 } } } In query syntax using System; using System.Collections; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] names = { "C", "Java", "C#", "Javascript" }; IEnumerable<string> query = from n in names where n.Contains("a") // Filter elements orderby n.Length // Sort elements select n.ToUpper(); // Translate each element (project) foreach (string name in query) Console.WriteLine(name); } } The output: JAVA JAVASCRIPT