using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
string[] names = { "J", "P", "G", "P" };
IEnumerable<string> outerQuery = names
.Where(n => n.Length == names.OrderBy(n2 => n2.Length)
.Select(n2 => n2.Length).First());
}
}
Use First to return the first matching element as a Product
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
List
Product product12 = (
from p in products
where p.Id == 2
select p)
.First();
Console.Write(product12.Category);
}
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;
}
}
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]
Extension Methods Discoverability
using System;
class Class1 {
}
class Class2 {
public void Method1(string s) {
Console.WriteLine("Class2.Method1");
}
}
class Class3 {
public void Method1(object o) {
Console.WriteLine("Class3.Method1");
}
}
class Class4 {
public void Method1(int i) {
Console.WriteLine("Class4.Method1");
}
}
static class Extensions {
static public void Method1(this object o, int i) {
Console.WriteLine("Extensions.Method1");
}
static void Main() {
new Class1().Method1(12); // Extensions.Method1 is called
new Class2().Method1(12); // Extensions.Method1 is called
new Class3().Method1(12); // Class3.Method1 is called
new Class4().Method1(12); // Class4.Method1 is called
}
}
Object Initializer
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; }
}
public class MainClass {
static void Main(string[] args) {
var processes = new List<MyPC>();
foreach (var process in Process.GetProcesses()) {
processes.Add(new MyPC {
Id = process.Id,
Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
}
}
Use delegate to create a filter
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
class Person {
int _id;
int _idRole;
string _lastName;
string _firstName;
public int ID {
get { return _id; }
set { _id = value; }
}
public int IDRole {
get { return _idRole; }
set { _idRole = value; }
}
public string LastName {
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}
}
class Program {
static void Main(string[] args) {
List<Person> people = new List<Person> {
new Person { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
new Person { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
};
Func<Person, bool> filter = delegate(Person p) { return p.ID == 1; };
var query = people
.Where(filter)
.Select(p => new { p.FirstName, p.LastName });
}
}
Calling the Common Library Filter Method
using System.Collections;
using System;
public delegate bool IntFilter(int i);
public class MainClass {
public static void Main() {
int[] nums = { 1, 2, 3, 4, 5, 6};
int[] oddNums = FilterArrayOfInts(nums, IsOdd);
foreach (int i in oddNums)
Console.WriteLine(i);
}
public static bool IsOdd(int i) {
return ((i & 1) == 1);
}
public static int[] FilterArrayOfInts(int[] ints, IntFilter filter) {
ArrayList aList = new ArrayList();
foreach (int i in ints) {
if (filter(i)) {
aList.Add(i);
}
}
return ((int[])aList.ToArray(typeof(int)));
}
}
Calling the Filter Method with an Anonymous Method
using System;
using System.Collections;
public delegate bool IntFilter(int i);
public class MainClass {
public static int[] FilterArrayOfInts(int[] ints, IntFilter filter) {
ArrayList aList = new ArrayList();
foreach (int i in ints) {
if (filter(i)) {
aList.Add(i);
}
}
return ((int[])aList.ToArray(typeof(int)));
}
public static void Main() {
int[] nums = { 1, 2, 3, 4, 5, 6};
int[] oddNums = FilterArrayOfInts(nums, delegate(int i) { return ((i & 1) == 1); });
foreach (int i in oddNums)
Console.WriteLine(i);
}
}

