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("000271",new MyStringifiedNumberComparer()); Console.WriteLine(contains); } }
LINQ
Contains 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"}; bool contains = presidents.Contains("H"); Console.WriteLine(contains); } }
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 } } }
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 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); } }
Uses Concat to merge the names of all customers and products
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
List
var customerNames =
from c in customers
select c.CompanyName;
var productNames =
from p in products
select p.ProductName;
var allNames = customerNames.Concat(productNames);
Console.WriteLine(“Customer and product names:”);
foreach (var n in allNames) {
Console.WriteLine(n);
}
}
static List
List
empTree.Add(new Product { ProductName = “A”, Category = “O”, UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “B”, Category = “O”, UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 2 });
empTree.Add(new Product { ProductName = “C”, Category = “O”, UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 3 });
empTree.Add(new Product { ProductName = “D”, Category = “O”, UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 4 });
empTree.Add(new Product { ProductName = “E”, Category = “O”, UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 5 });
empTree.Add(new Product { ProductName = “F”, Category = “O”, UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 6 });
return empTree;
}
static List
List
empTree.Add(new Product { ProductName = “A”, Category = “O”, UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “B”, Category = “O”, UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “C”, Category = “O”, UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “D”, Category = “O”, UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “E”, Category = “O”, UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
empTree.Add(new Product { ProductName = “F”, Category = “O”, UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 });
List
l.Add(new Customer { CompanyName = “A”, Region = “R1”, UnitsInStock = 1, Orders = empTree, CustomerId = 0 });
l.Add(new Customer { CompanyName = “B”, Region = “R2”, UnitsInStock = 2, Orders = empTree, CustomerId = 1 });
l.Add(new Customer { CompanyName = “C”, Region = “R3”, UnitsInStock = 3, Orders = empTree, CustomerId = 2 });
l.Add(new Customer { CompanyName = “D”, Region = “R4”, UnitsInStock = 4, Orders = empTree, CustomerId = 3 });
l.Add(new Customer { CompanyName = “E”, Region = “R5”, UnitsInStock = 5, Orders = empTree, CustomerId = 4 });
return l;
}
}
class Customer : IComparable
public string CompanyName { get; set; }
public string Region { get; set; }
public List
public int UnitsInStock { get; set; }
public int CustomerId { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1}, Region: {3}”, this.CustomerId, this.CompanyName, this.Region);
}
int IComparable
if (other == null)
return 1;
if (this.CustomerId > other.CustomerId)
return 1;
if (this.CustomerId < other.CustomerId)
return -1;
return 0;
}
}
class Product : IComparable
public string ProductName { get; set; }
public string Category { get; set; }
public int UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public int Total { get; set; }
public DateTime OrderDate { get; set; }
public int Id { get; set; }
public override string ToString() {
return String.Format(“Id: {0}, Name: {1} , Category: {3}”, this.Id, this.ProductName, this.Category);
}
int IComparable
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;
if (this.Id < other.Id) return -1; return 0; } } [/csharp]
Use Concat to merge two integer arrays into a single sequence
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 }; var allNumbers = numbersA.Concat(numbersB); Console.WriteLine("All numbers from both arrays:"); foreach (var n in allNumbers) { Console.WriteLine(n); } } }