Using var

   
 

using System;
using System.Collections.Generic;
using System.Diagnostics;

class MyPC {
    public Int32 Id { get; set; }
    public Int64 Memory { get; set; }
    public String Name { get; set; }
}

class LanguageFeatures {
    static void Main(string[] args) {
        var processes = new List<MyPC>();
        foreach (var process in Process.GetProcesses()) {
            var data = new MyPC();
            data.Id = process.Id;
            data.Name = process.ProcessName;
            data.Memory = process.WorkingSet64;
            processes.Add(data);
        }
        Console.Write(processes);
    }
}

    


Union Operator

   
 

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> first = presidents.Take(5);
        IEnumerable<string> second = presidents.Skip(4);
        IEnumerable<string> concat = first.Concat<string>(second);
        IEnumerable<string> union = first.Union<string>(second);

        Console.WriteLine("The count of the array is: " + presidents.Count());
        Console.WriteLine("The count of the first sequence is: " + first.Count());
        Console.WriteLine("The count of the second sequence is: " + second.Count());
        Console.WriteLine("The count of the concat sequence is: " + concat.Count());
        Console.WriteLine("The count of the union sequence is: " + union.Count());

    }
}

    


prints unique letters from Product and Customer names

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
public static void Main() {

List products = GetProductList();
List customers = GetCustomerList();

var productFirstChars =
from p in products
select p.ProductName[0];
var customerFirstChars =
from c in customers
select c.CompanyName[0];

var uniqueFirstChars = productFirstChars.Union(customerFirstChars);

Console.WriteLine(“Unique first letters from Product names and Customer names:”);
foreach (var ch in uniqueFirstChars) {
Console.WriteLine(ch);
}
}
static List GetProductList() {
List empTree = new 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 });
return empTree;
}
static List GetCustomerList() {
List empTree = new 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 = new 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 Orders { get; set; }
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.CompareTo(Customer other) {
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.CompareTo(Product other) {
if (other == null)
return 1;
if (this.Id > other.Id)
return 1;

if (this.Id < other.Id) return -1; return 0; } } [/csharp]

prints the unique elements of two integer arrays

   
 
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 uniqueNumbers = numbersA.Union(numbersB);

        Console.WriteLine("Unique numbers from both arrays:");
        foreach (var n in uniqueNumbers) {
            Console.WriteLine(n);
        }
    }
}

    


Use Linq to union two arrays

   
 
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, 1, 3, 3};
            int[] numbers2 = {1, 2, 3, 4};
            Console.Write(numbers.Union(numbers2));
   }
}

    


Use ToLookup to search object property

   
 

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Employee {
    public int birthYear;
    public string firstName;
    public string lastName;

    public static Employee[] GetEmployees() {
        Employee[] actors = new Employee[] {
       new Employee { birthYear = 1964, firstName = "K", lastName = "R" },
       new Employee { birthYear = 1968, firstName = "O", lastName = "W" },
       new Employee { birthYear = 1960, firstName = "J", lastName = "S" },
       new Employee { birthYear = 1964, firstName = "S", lastName = "B" },
    };

        return (actors);
    }
}
public class MainClass {
    public static void Main() {
        ILookup<int, Employee> lookup = Employee.GetEmployees().ToLookup(k => k.birthYear);
        IEnumerable<Employee> actors = lookup[1964];
        foreach (var actor in actors)
            Console.WriteLine("{0} {1}", actor.firstName, actor.lastName);
    }
}