using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MyStringifiedNumberComparer : IEqualityComparer<string> { public bool Equals(string x, string y) { return (Int32.Parse(x) == Int32.Parse(y)); } public int GetHashCode(string obj) { return Int32.Parse(obj).ToString().GetHashCode(); } } public class MainClass { public static void Main() { string[] stringifiedNums = {"001", "49", "017", "0080", "00027", "2" }; bool contains = stringifiedNums.Contains("2",new MyStringifiedNumberComparer()); Console.WriteLine(contains); } }
A query to extract strings containing the letter “a”, sorted by length and converted to uppercase
using System; using System.Collections.Generic; using System.Linq; class LinqDemo { static void Main() { string[] names = { "J", "P", "G", "Pa" }; IEnumerable<string> query = from n in names where n.Contains("a") orderby n.Length select n.ToUpper(); foreach (string name in query) Console.Write(name + "/"); } }
Count with string value
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; int count = presidents.Count(); Console.WriteLine(count); } }
Count with string operator
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; int count = presidents.Count(s => s.StartsWith("H")); Console.WriteLine(count); } }
Concat Prototype
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"ant", "arding", "arrison", "eyes", "over", "ackson"}; IEnumerable<string> items = presidents.Take(5).Concat(presidents.Skip(5)); foreach (string item in items) Console.WriteLine(item); } }
Concatention by Using the Concat Operator
using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass { public static void Main() { string[] presidents = {"ant", "arding", "rison", "eyes", "over", "ackson"}; IEnumerable<string> items = new[] { presidents.Take(5), presidents.Skip(5) }.SelectMany(s => s); foreach (string item in items) Console.WriteLine(item); } }
Concat appends one sequence to another
using System; using System.Collections.Generic; using System.Linq; public class MainClass { public 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 } } }