LinqSandboxEntities1 sandboxDb = new LinqSandboxEntities1(); //from edmx

//select
var mySelect = from person in sandboxDb.People
select person;

foreach (var p in mySelect)
{
Console.WriteLine(p.Name);
}
Console.WriteLine();

//select new
var myNew = from person in sandboxDb.People
select new { Person = person };

foreach (var p in myNew)
{
Console.WriteLine(p.Person.Name);
}
Console.WriteLine();

//orderby
var myOrderby = from person in sandboxDb.People
orderby person.Name ascending
select person;

foreach (var p in myOrderby)
{
Console.WriteLine(p.Name);
}
Console.WriteLine();

//group
var myGroup = from pet in sandboxDb.Pets
group pet by pet.Species;

foreach (var p in myGroup)
{
Console.WriteLine(p.Key + “: ” + p.Count());
}
Console.WriteLine();

//group into
var myGroupInto = from pet in sandboxDb.Pets
group pet by pet.Species
into g
select new { Species = g.Key, Count = g.Count() };

foreach (var p in myGroupInto)
{
Console.WriteLine(p.Species + “: ” + p.Count);
}
Console.WriteLine();

//let
var myLet = from person in sandboxDb.People
let ageInDecades = person.Age / 10.0
let name = person.Name
where ageInDecades != null
select new { AgeInDecades = ageInDecades, PersonName = name };

foreach (var p in myLet)
{
Console.WriteLine(p.PersonName + “‘s age measured in decades: ” + p.AgeInDecades);
}
Console.WriteLine();

//join
var myJoin = from person in sandboxDb.People
join map in sandboxDb.PersonPetMappings
on person.PersonId equals map.PersonId
join pet in sandboxDb.Pets
on map.PetId equals pet.PetId
select new { PersonName = person.Name, PetName = pet.Name };

foreach (var j in myJoin)
{
Console.WriteLine(j.PersonName + ” owns ” + j.PetName);
}
Console.WriteLine();

//DefaultIfEmpty1
List list1 = new List();
var myDefaultifempty1 = list1.DefaultIfEmpty(“list is empty”);

foreach (var s in myDefaultifempty1)
{
Console.WriteLine(s);
}
Console.WriteLine();

//DefaultIfEmpty2
List list2 = new List();
var myDefaultifempty2 = list2.DefaultIfEmpty();

foreach (var s in myDefaultifempty2)
{
Console.WriteLine(“\”” + s + “\””);
}
Console.WriteLine();

//FirstOrDefault
var myFirstOrDefault = from person in sandboxDb.People
//where person.Name == “Lemonjello”
select person.Name;

Console.WriteLine(myFirstOrDefault.FirstOrDefault());
Console.WriteLine();

//Average
//Count
//Sum
//Min
//Max

//GroupJoin
//Seems to be useful for joining classes which contain
//collections, and scalar attributes which can be joined upon
//http://geekswithblogs.net/Martinez/archive/2010/01/27/groupjoin-method-tutorial.aspx

//AsParallel
//NEVER USE ASPARALLEL() WITH A STATIC METHOD
//If used right, this speeds up execution by using multiple processors core threads

//Cast
//Is useful for casting a child class as a parent class

//Concat
int[] array1 = { 1, 3, 5 };
int[] array2 = { 0, 2, 4 };

var myConcat = array1.Concat(array2);
foreach (int i in myConcat)
{
Console.WriteLine(i);
}
Console.WriteLine();

//Contains
var list3 = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bool a = list3.Contains(7);

Console.WriteLine(a);
Console.WriteLine();

//Distinct
int[] array3 = { 1, 2, 2, 3, 4, 4 };
var myDistinct = from map in sandboxDb.PersonPetMappings
join pet in sandboxDb.Pets
on map.PetId equals pet.PetId
select pet.Name;
foreach (string n in myDistinct.Distinct())
{
Console.WriteLine(n);
}
Console.WriteLine();

//Except
var myExcept1 = from pet in sandboxDb.Pets
select pet;
var myExcept2 = from pet in sandboxDb.Pets
where pet.Species == “Cat”
select pet;

foreach (Pet d in myExcept1.Except(myExcept2))
{
Console.WriteLine(d.Name + ” is a ” + d.Species);
}
Console.WriteLine();

//Zip
var array4 = new string[] { “Donald”, “Mickey”, “Pluto” };
var array5 = new string[] { “Duck”, “Mouse”, “Dog” };
var myZip = array4.Zip(array5, (a, b) => (a + ” ” + b));

foreach (var n in myZip)
{
Console.WriteLine(n);
}
Console.WriteLine();

//Where1
var array6 = new int[] { 1, 2, 3, 4, 5 };
var myWhere1 = array6.Where(i => i % 2 != 0);
foreach (int o in myWhere1)
{
Console.WriteLine(o + ” is an odd number”);
}
Console.WriteLine();

//Where2
var myWhere2 = from pet in sandboxDb.Pets
select pet;

foreach (Pet d in myWhere2.Where(p => p.Species == “Dog”))
{
Console.WriteLine(d.Name + ” is an dog”);
}
Console.WriteLine();

//SelectMany
string[] array7 = new string[] { “123”, “456”, “789” };
var mySelectMany = array7.SelectMany(element => element.ToCharArray());

foreach (char n in mySelectMany)
{
Console.WriteLine(n);
}
Console.WriteLine();

//OfType
object[] array8 = new object[4];
array8[0] = new StringBuilder();
array8[1] = “example”;
array8[2] = new int[1];
array8[3] = “another”;

// Filter the objects by their type.
// … Only match strings.
// … Print those strings to the screen.
var result = array8.OfType();
foreach (var element in result)
{
Console.WriteLine(element);
}
Console.WriteLine();

//Single
var mySingle = from person in sandboxDb.People
join map in sandboxDb.PersonPetMappings
on person.PersonId equals map.PersonId
join pet in sandboxDb.Pets
on map.PetId equals pet.PetId
select new { PersonName = person.Name, PetName = pet.Name };

try
{
var onwerOfOnlyOnePet = mySingle.Single(j => j.PersonName == “Tom”);
}
catch (Exception ex)
{
//if this code is reached then Barbara owns more than one pet
Console.WriteLine(ex.GetType());
}

//Aggregate1
string[] array9 = { “take”, “me”, “to”, “your”, “leader” };
var myAggregate1 = array9.Aggregate((a, b) => a + ” ” + b);

Console.WriteLine(myAggregate1);

//Aggregate2
int[] array10 = { 1, 2, 3, 4 };
var myAggregate2 = array10.Aggregate((a, b) => a + b);

Console.WriteLine(myAggregate2);

//All
var p = from pet in sandboxDb.Pets
select pet;

Console.WriteLine(p.All(d => d.Species == “Dog”));
Console.WriteLine();

//Any
var p = from pet in sandboxDb.Pets
select pet;

Console.WriteLine(p.Any(d => d.Species == “Dog”));
Console.WriteLine();

//SequenceEqual
int[] array11 = { 1, 2, 3 };
int[] array12 = { 1, 1 + 1, 6 / 2 };

Console.WriteLine(array11.SequenceEqual(array12));
Console.WriteLine();

//Empty
var myEmpty = Enumerable.Empty();

Console.WriteLine(“my empty enumerable count: ” + myEmpty.Count());
Console.WriteLine();

//Range
var myRange = Enumerable.Range(7, 3);

foreach (int i in myRange)
{
Console.WriteLine(i);
}

//Repeat
var myRepeat = Enumerable.Repeat(“Hello My Name Is: “, 5);

foreach (string s in myRepeat)
{
Console.WriteLine(s);
}

//LookUp
var p = from person in sandboxDb.People
select person.Name;
var myLookUp = p.ToLookup(n => n.Length);

foreach (var grouping in myLookUp)
{
Console.WriteLine(“People with a name length of: ” + grouping.Key);
foreach (string n in grouping)
{
Console.WriteLine(n);
}
}
Console.WriteLine();

//ToDictionary
var p = from person in sandboxDb.People
select person;
List myToDictionary = p.ToList();
Dictionary myDictionary = myToDictionary.ToDictionary(k => k.Name, v => v);

Console.WriteLine(myDictionary[“Brian”].Name + “‘s age: ” + myDictionary[“Brian”].Age);
Console.WriteLine();

//SkipWhile
object[] array14 = { new Person(), new Pet(), “a”, “b”, “c” };
var mySkipWhile = array14.SkipWhile(a => a.GetType() != typeof(string));

foreach (string s in mySkipWhile)
{
Console.WriteLine(s);
}
Console.WriteLine();

//Take
object[] array15 = { 1, 2, 3, new Person(), new Pet() };
var myTake = array15.Take(2);

foreach (int i in myTake)
{
Console.WriteLine(i * 11);
}
Console.WriteLine();

//TakeWhile
object[] array16 = { “a”, “b”, “c”, new Person(), new Pet() };
var myTakeWhile = array16.TakeWhile(a => a.GetType() == typeof(string));

foreach (string s in myTakeWhile)
{
Console.WriteLine(s);
}
Console.WriteLine();