using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
string[] presidents = {"A", "A", "B", "Bu", "Car", "land"};
IEnumerable<string> items = presidents.Take(5);
foreach (string item in items)
Console.WriteLine(item);
}
}
Month: February 2011
Sum with object property
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 EmployeeOptionEntry {
public int id;
public long optionsCount;
public DateTime dateAwarded;
public static EmployeeOptionEntry[] GetEmployeeOptionEntries() {
EmployeeOptionEntry[] empOptions = new EmployeeOptionEntry[] {
new EmployeeOptionEntry {
id = 1,
optionsCount = 2,
dateAwarded = DateTime.Parse("1999/12/31") },
new EmployeeOptionEntry {
id = 101,
optionsCount = 2,
dateAwarded = DateTime.Parse("1998/12/31") }
};
return (empOptions);
}
}
public class MainClass {
public static void Main() {
IEnumerable<EmployeeOptionEntry> options = EmployeeOptionEntry.GetEmployeeOptionEntries();
long optionsSum = options.Sum(o => o.optionsCount);
Console.WriteLine("The sum of the employee options is: {0}", optionsSum);
}
}
use Linq Sum to sum an array
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
public class MainClass{
public static void Main(){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var query = numbers.Sum();
}
}
use Sum to find the total of all of the numbers in an integer 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, 2, 0 };
double numSum = numbers.Sum();
Console.WriteLine("The sum of the numbers is {0}.", numSum);
}
}
Sum with Projection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
string[] words = { "cherry", "apple", "blueberry" };
double totalChars = words.Sum(w => w.Length);
Console.WriteLine("There are a total of {0} characters in these words.", totalChars);
}
}
Sum with integers
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
IEnumerable<int> ints = Enumerable.Range(1, 10);
foreach (int i in ints)
Console.WriteLine(i);
int sum = ints.Sum();
Console.WriteLine(sum);
}
}
A subquery is a query contained within another query's lambda expression.
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
string[] musos = { "D G", "R W", "R W" };
IEnumerable<string> query = musos.OrderBy(m => m.Split().Last());
}
}

