C# Professional - LINQ - Exercices
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
LINQ Examples
Use of Let and Select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace LinqQueries
{
class Program
{
static void Main(string[] args)
{
// }
List<Element> elements = BuildList();
// LINQ Query.
var subset = from theElement in elements
let specificElement = theElement.Name.ToLower()
where specificElement.Contains("ca")
select theElement;
foreach (Element theElement in subset)
{
Console.WriteLine(theElement.Name + " " + theElement.AtomicNumber);
}
}
private static List<Element> BuildList()
{
return new List<Element>
{
{ new Element() { Symbol="K", Name="Potassium", AtomicNumber=19}},
{ new Element() { Symbol="Ca", Name="Calcium", AtomicNumber=20}},
{ new Element() { Symbol="Sc", Name="Scandium", AtomicNumber=21}},
{ new Element() { Symbol="Ti", Name="Titanium", AtomicNumber=22}},
{ new Element() { Symbol="V", Name="Vanadium", AtomicNumber=23}},
{ new Element() { Symbol="Cr", Name="Chromium", AtomicNumber=24}}
};
}
// {
public class Element
{
public string Symbol { get; set; }
public string Name { get; set; }
public int AtomicNumber { get; set; }
}
}
}
// }
Enter to Rename, Shift+Enter to Preview
Use of orderby/groupBy
1
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
59
// {
List<Element> elements = BuildList();
// LINQ Query.
var subset = from theElement in elements
group theElement by theElement.Family into familyGroup
orderby familyGroup.Key
select familyGroup;
foreach (var familyGrp in subset)
{
Console.WriteLine("Family: {0}", familyGrp.Key);
foreach (var element in familyGrp)
{
Console.WriteLine("\t- {0}",element.Name);
}
}
}
private static List<Element> BuildList()
{
return new List<Element>
{
{ new Element() { Family="Noble gas", Symbol="He", Name="Helium", AtomicNumber=2}},
{ new Element() { Family="Halogen", Symbol="F", Name="Fluorine", AtomicNumber=9}},
{ new Element() { Family="Noble gas", Symbol="Ne", Name="Neon", AtomicNumber=10}},
{ new Element() { Family="Halogen", Symbol="Cl", Name="Chlorine", AtomicNumber=17}},
{ new Element() { Family="Noble gas", Symbol="Ar", Name="Argon", AtomicNumber=18}},
{ new Element() { Family="Alkali metal", Symbol="Na", Name="Sodium", AtomicNumber=11}},
{ new Element() { Family="Noble gas", Symbol="Rn", Name="Radon", AtomicNumber=86}},
{ new Element() { Family="Alkali metal", Symbol="K", Name="Potassium", AtomicNumber=19}},
{ new Element() { Family="Halogen", Symbol="At", Name="Astatine", AtomicNumber=85}},
{ new Element() { Family="Alkali metal", Symbol="Li", Name="Lithium", AtomicNumber=3}}
};
}
// {
Enter to Rename, Shift+Enter to Preview
Useful extension methods
Cout()
1
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
58
// {
List<Element> elements = BuildList();
// LINQ Query.
var subset = from theElement in elements
group theElement by theElement.Family into familyGroup
select new
{
Name = familyGroup.Key,
Count = familyGroup.Count()
};
foreach (var familyGrp in subset)
{
Console.WriteLine("The {0} family has {1} members in the list.", familyGrp.Name, familyGrp.Count);
}
}
private static List<Element> BuildList()
{
return new List<Element>
{
{ new Element() { Family="Noble gas", Symbol="He", Name="Helium", AtomicNumber=2}},
{ new Element() { Family="Halogen", Symbol="F", Name="Fluorine", AtomicNumber=9}},
{ new Element() { Family="Noble gas", Symbol="Ne", Name="Neon", AtomicNumber=10}},
{ new Element() { Family="Halogen", Symbol="Cl", Name="Chlorine", AtomicNumber=17}},
{ new Element() { Family="Noble gas", Symbol="Ar", Name="Argon", AtomicNumber=18}},
{ new Element() { Family="Alkali metal", Symbol="Na", Name="Sodium", AtomicNumber=11}},
{ new Element() { Family="Noble gas", Symbol="Rn", Name="Radon", AtomicNumber=86}},
{ new Element() { Family="Alkali metal", Symbol="K", Name="Potassium", AtomicNumber=19}},
{ new Element() { Family="Halogen", Symbol="At", Name="Astatine", AtomicNumber=85}},
{ new Element() { Family="Alkali metal", Symbol="Li", Name="Lithium", AtomicNumber=3}}
};
}
// {
Enter to Rename, Shift+Enter to Preview
Skip()
1
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
54
// {
List<Element> elements = BuildList();
// LINQ Query.
var subset = (from theElement in elements
orderby theElement.Name
select theElement).Skip(7);
foreach (var element in subset)
{
Console.WriteLine(element.Name);
}
}
private static List<Element> BuildList()
{
return new List<Element>
{
{ new Element() { Family="Noble gas", Symbol="He", Name="Helium", AtomicNumber=2}},
{ new Element() { Family="Halogen", Symbol="F", Name="Fluorine", AtomicNumber=9}},
{ new Element() { Family="Noble gas", Symbol="Ne", Name="Neon", AtomicNumber=10}},
{ new Element() { Family="Halogen", Symbol="Cl", Name="Chlorine", AtomicNumber=17}},
{ new Element() { Family="Noble gas", Symbol="Ar", Name="Argon", AtomicNumber=18}},
{ new Element() { Family="Alkali metal", Symbol="Na", Name="Sodium", AtomicNumber=11}},
{ new Element() { Family="Noble gas", Symbol="Rn", Name="Radon", AtomicNumber=86}},
{ new Element() { Family="Alkali metal", Symbol="K", Name="Potassium", AtomicNumber=19}},
{ new Element() { Family="Halogen", Symbol="At", Name="Astatine", AtomicNumber=85}},
{ new Element() { Family="Alkali metal", Symbol="Li", Name="Lithium", AtomicNumber=3}}
};
}
// {
Enter to Rename, Shift+Enter to Preview
Join
1
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// {
List<Element> elementsNames = BuildList();
List<SymbolElement> elementsSymbols = BuildListSymbols();
// LINQ Query.
var subset = from theElement in elementsNames
join sym in elementsSymbols on theElement.AtomicNumber equals sym.AtomicNumber
where theElement.AtomicNumber == 17
select new {ElementSymbol = sym.Symbol, ElementName =theElement.Name};
foreach (var element in subset)
{
Console.WriteLine("The {0} element symbol is : {1}",element.ElementName,element.ElementSymbol);
}
Console.ReadKey();
}
private static List<Element> BuildList()
{
return new List<Element>
{
{ new Element() { Name="Helium", AtomicNumber=2}},
{ new Element() { Name="Fluorine", AtomicNumber=9}},
{ new Element() { Name="Neon", AtomicNumber=10}},
{ new Element() { Name="Chlorine", AtomicNumber=17}},
{ new Element() { Name="Argon", AtomicNumber=18}},
{ new Element() { Name="Sodium", AtomicNumber=11}},
{ new Element() { Name="Radon", AtomicNumber=86}},
{ new Element() { Name="Potassium", AtomicNumber=19}},
{ new Element() { Name="Astatine", AtomicNumber=85}},
{ new Element() { Name="Lithium", AtomicNumber=3}}
};
}
private static List<SymbolElement> BuildListSymbols()
{
return new List<SymbolElement>
{
{ new SymbolElement() { Symbol="He", AtomicNumber=2}},
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content