using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"A", "Art", "Buch", "Bush", "Car", "land"};
IEnumerable<string> items = presidents.Skip(1);
foreach (string item in items)
Console.WriteLine(item);
}
}
Month: February 2011
uses Skip to get all but the first 4 elements of the array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass{
public static void Main() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7};
var allButFirst4Numbers = numbers.Skip(4);
Console.WriteLine("All but first 4 numbers:");
foreach (var n in allButFirst4Numbers) {
Console.WriteLine(n);
}
}
}
Nested Skip
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
var waOrders =
from c in customers
from o in c.Orders
where c.Region == “R1”
select new { c.CustomerId, o.Id, o.OrderDate };
var allButFirst2Orders = waOrders.Skip(2);
Console.WriteLine(“All but first 2 orders in R1:”);
foreach (var order in allButFirst2Orders) {
Console.WriteLine(order);
}
}
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 Skip
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, 5, 6, 7, 8, 9 };
var query = numbers.Take(5);
var query2 = numbers.Skip(5);
}
}
The Skip operator ignores the first x elements and outputs the rest
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
int[] numbers = { 10, 9, 8, 7, 6 };
IEnumerable<int> lastTwo = numbers.Skip(3); // { 7, 6 }
}
}
SingleOrDefault: Where an Element Is Not Found
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Employee {
public int id;
public string firstName;
public string lastName;
public static ArrayList GetEmployeesArrayList() {
ArrayList al = new ArrayList();
al.Add(new Employee { id = 1, firstName = "J", lastName = "R" });
al.Add(new Employee { id = 2, firstName = "W", lastName = "G" });
al.Add(new Employee { id = 3, firstName = "A", lastName = "H" });
al.Add(new Employee { id = 4, firstName = "D", lastName = "L" });
al.Add(new Employee { id = 101, firstName = "K", lastName = "F" });
return (al);
}
public static Employee[] GetEmployeesArray() {
return ((Employee[])GetEmployeesArrayList().ToArray(typeof(Employee)));
}
}
public class MainClass {
public static void Main() {
Employee emp = Employee.GetEmployeesArray().Where(e => e.id == 5).SingleOrDefault();
Console.WriteLine(emp == null ? "NULL" :
string.Format("{0} {1}", emp.firstName, emp.lastName));
}
}
SingleOrDefault after where clause
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Employee {
public int id;
public string firstName;
public string lastName;
public static ArrayList GetEmployeesArrayList() {
ArrayList al = new ArrayList();
al.Add(new Employee { id = 1, firstName = "J", lastName = "R" });
al.Add(new Employee { id = 2, firstName = "W", lastName = "G" });
al.Add(new Employee { id = 3, firstName = "A", lastName = "H" });
al.Add(new Employee { id = 4, firstName = "D", lastName = "L" });
al.Add(new Employee { id = 101, firstName = "K", lastName = "F" });
return (al);
}
public static Employee[] GetEmployeesArray() {
return ((Employee[])GetEmployeesArrayList().ToArray(typeof(Employee)));
}
}
public class MainClass {
public static void Main() {
Employee emp = Employee.GetEmployeesArray().Where(e => e.id == 4).SingleOrDefault();
Console.WriteLine(emp == null ? "NULL" :
string.Format("{0} {1}", emp.firstName, emp.lastName));
}
}

