using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class CaseInsensitiveComparer : IComparer<string> {
public int Compare(string x, string y) {
return string.Compare(x, y, true);
}
}
public class MainClass {
public static void Main() {
string[] words = { "a", "A", "b", "B", "C", "c" };
var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer());
}
}
Month: February 2011
OrderBy: prints an alphabetically sorted version of a string array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
string[] words = { "C", "a", "b" };
var sortedWords =
from w in words
orderby w
select w;
Console.WriteLine("The sorted list of words:");
foreach (var w in sortedWords) {
Console.WriteLine(w);
}
}
}
string array sorted by the length each element
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
string[] words = { "abc", "bcd", "def" };
var sortedWords =
from w in words
orderby w.Length
select w;
Console.WriteLine("The sorted list of words (by length):");
foreach (var w in sortedWords) {
Console.WriteLine(w);
}
}
}
an untidy nested query
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
public static void Main() {
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query =
Enumerable.Select(
Enumerable.OrderBy(
Enumerable.Where(
names, n => n.Contains("a")
), n => n.Length
), n => n.ToUpper()
);
}
}
Query an array of object by its property value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
public class Book {
public String Title { get; set; }
public override String ToString() {
return Title;
}
}
class Program {
static void Main(string[] args) {
Book[] books = {
new Book { Title="A" },
new Book { Title="F" },
new Book { Title="E" } };
var titles =
books
.Where(book => book.Title.Contains("A"))
.Select(book => book.Title);
}
}
Select object in a List
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
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"},
new Person { ID = 3, IDRole = 2, LastName = "G", FirstName = "M"},
new Person { ID = 4, IDRole = 3, LastName = "C", FirstName = "G"}
};
var query = from p in people select p;
}
}
Query by Person's FirstName
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
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"},
new Person { ID = 3, IDRole = 2, LastName = "G", FirstName = "M"},
new Person { ID = 4, IDRole = 3, LastName = "C", FirstName = "G"}
};
var query = from p in people
where p.FirstName == "B"
select p;
}
}

