Filter array of strings - only strings starting with 'd' (2 syntax options)

var selection = (from s in codes where s.StartsWith("d") select s).ToList();

var selection = codes.Where(s => s.StartsWith("d")).ToList();


More info and samples on: www.devarchweb.net

Order array of strings by second and then first characters of each element (2 syntax options)

var selection = (from s in codes where s.StartsWith("d") orderby s[1],s[0] descending select s).ToList();

var selection = codes.Where(s => s.StartsWith("d")).OrderBy(s => s[1]).ThenByDescending(s => s[0]).ToList();


More info and samples on: www.devarchweb.net

Select people from IT department (2 syntax options)

// select whole classes from the list
List<Person> itPeople = people.Where(p => p.DepartmentId == "IT").ToList();

// if you want only people names
List<string> itPeopleNames = people.Where(p => p.DepartmentId == "IT").Select(p => p.Name).ToList();


More info and samples on: www.devarchweb.net

Group and list people by department (2 syntax options)

var groups = from p in people group p by p.DepartmentId into g select new { MyKey = g.Key, MyGroup = g };

var groups1 = people.GroupBy(p => p.DepartmentId).Select( g => new { MyKey = g.Key, MyGroup = g });

// iterate over result
foreach (var gr in groups1)
{
  string key = gr.MyKey;
  foreach (var p in gr.MyGroup)
  {
    Person person = p;
  }
}


More info and samples on: www.devarchweb.net

Calculate average age for each department (2 syntax options)

var averages = from p in people group p by p.DepartmentId into g select new { MyKey = g.Key, AvgAge = g.Average(p => p.Age) };

var averages1 = people.GroupBy(p => p.DepartmentId).Select(g => new { MyKey = g.Key, MyAvg = g.Average(p => p.Age) });


More info and samples on: www.devarchweb.net

Calculate salary average for department and age (Procedural syntax)

var averages2 = people
  .GroupBy(p => new { p.DepartmentId, p.Age })
  .Select(g => new { DeptId = g.Key.DepartmentId, Age = g.Key.Age, Salary = g.Average(p => p.Salary) });


More info and samples on: www.devarchweb.net

Select distinct names and departments (using grouping)

var disctincList = people
 .GroupBy(p => new { p.Name, p.DepartmentId })
 .Select(g => g.First());


More info and samples on: www.devarchweb.net

Select disctinct set (2 syntax options)

IEnumerable<Person> disctinctSet = people.Distinct();

IEnumerable<Person> disctinctSet1 = (from p in people select p).Distinct();


More info and samples on: www.devarchweb.net

First, FirstOrDefault, Single

First - returns first element of sequence. Throws exception when the sequence is empty
FirstOrDefault - returns first element of sequence. If the sequence is empty, returns an empty object
Single - returns first element of sequence. Throws exception when the sequence is empty or has more than 1 element


More info and samples on: www.devarchweb.net

How to write for each in LINQ

foreach (var p in persons)
{
  p.LastName = "John";
}



it is possible to see foreach written in LINQ as below

persons.Select(p => { p.LastName = "John"; return p; } ).ToList();

persons.All(p => { p.LastName = "John"; return true; } ).ToList();


More info and samples on: www.devarchweb.net

Join Person and Department classes (2 syntax options)

// join two object sets - option 1
IEnumerable<PersonWithDepartment> joinedSet = from d in departments
                       join p in people on d.DepartmentId equals p.DepartmentId
                       select new PersonWithDepartment()
                       {
                         DepartmentId = p.DepartmentId,
                         PersonName = p.Name,
                         DepartmentName = d.Name
                       };

// join two object sets - option 2
IEnumerable<PersonWithDepartment> joinedSet2 = people.Join(
  departments,
  (p) => (p.DepartmentId),
  (d) => (d.DepartmentId),
  (p, d) => new PersonWithDepartment()
  {
    DepartmentId = p.DepartmentId,
    PersonName = p.Name,
    DepartmentName = d.Name
  }
);


More info and samples on: www.devarchweb.net

Left outer join for Person and Department classes

var outerJoinSet = from p in people
          join d in departments on p.DepartmentId equals d.DepartmentId into d1
          from d2 in d1.DefaultIfEmpty()

          select new
          {
            DepartmentId = p.DepartmentId,
            PersonName = p.Name,
            DepartmentName = d2.Name
          };


More info and samples on: www.devarchweb.net

Add new person to the List of person or combine 2 lists (3 options)

// create NEW set as union of two sets
List<Person> union = people.Union(
  new List<Person>() {new Person() { DepartmentId = "IT", Name = "George" }}).ToList();

// add people to existing set
people.AddRange(
  new List<Person>() { new Person() { DepartmentId = "IT", Name = "George" } });


// concatenate existing 2 lists - the original lists remain untouched, no new Person element gets allocated
var list1 = new List<PersonO>() { new PersonO() { DepartmentId = "IT", Name = "John" } };
var list2 = new List<PersonO>() { new PersonO() { DepartmentId = "FI", Name = "George" } };
List<PersonO> concatenated = list1.Concat(list2).ToList();


More info and samples on: www.devarchweb.net

How SelectMany works

// cross join
var list1 = new string[] { "1", "2", "3" };
var list2 = new string[] { "A", "B" };

var result1 = list1.SelectMany(l1 => list2, (l1, l2) => new { l1, l2 });

// result: {1,A}, {1,B}, {2,A}, {2,B}, {3,A}, {3,B}


// list of lists flattening
var masterList = new List>string[]>() { list1, list2 };
var result2 = masterList.SelectMany(i => i);
// result 2: 1,2,3,A,B


More info and samples on: www.devarchweb.net

How Zip in works

string[] list1 = { "a", "b", "c" };
string[] list2 = { "X", "Y" };

var zipped = list1.Zip(list2, (i1, i2) => i1 + i2);

foreach (var item in zipped)
{
  Console.WriteLine(item);
}
// Output

// aX
// bY


More info and samples on: www.devarchweb.net

Calculate factorial. How to seed initial state.

var input = new []{1,2,3,4,5};
var result = input.Aggregate((a,b) => a * b);

// you can seed initial value
var result = input.Aggregate(10, (a,b) => a * b);


More info and samples on: www.devarchweb.net

Select object from list based on the object type

System.Collections.ArrayList objects = new System.Collections.ArrayList(2);
objects.Add("First");
objects.Add(1);
IEnumerable<string> strings = objects.OfType<string>();


More info and samples on: www.devarchweb.net

Sort list of sss_N{N} strings by N{N} and select N{N} > 2 Sometimes it is necessary to convert data before they reach filter and sort part of the query.

var list = new string[] { "abc_1", "def_11", "xyz_3" };
var result =
from item in list
let number = Convert.ToInt16(item.Split('_')[1])
where number > 2
orderby number
select item;

result = list
.Select(item => new { Item = item, Code = Convert.ToInt16(item.Split('_')[1]) })
.Where(i => i.Code > 2)
.OrderBy(i => i.Code)
.Select(i => i.Item);

// result: xyz_3, def_11


More info and samples on: www.devarchweb.net

How to debug LINQ query When it is necessary to debug a LINQ query if the where condition is more complicated that the one in this example.

var numbers = new int[] { 1, 2, 3 };

var result = numbers
.Where(n => n > 2);



It can be done without rewriting to foreach loop, but some code injection is necessary.

var result1 = numbers
.Select(
item =>
{
return item; // set breakpoint here - you will see 1,2,3
})
.Where(n => n > 2)
.Select(
item =>
{
return item; // set breakpoint here - you will see 3
});

// the breakpoint will be hit when code reaches ToList()
var result1debug = result1.ToList();


If you want to examine the the evaluation of the where condition

var result2 = numbers
.Where( n =>
{
bool b = n > 2;
return b; // set breakpoint here
});

var result2debug = result2.ToList();





More info and samples on: www.devarchweb.net