using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqExercises
{
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public char Gender { get; set; }
}
class Test
{
public void Exercise1() //integer array
{
int[] data = { 8, 4, 1, 2, 6, 5, 7, 9, 3 };
IEnumerable<int> queryvar = from rangevar in data
where rangevar % 2 == 0 && rangevar > 5
select rangevar;
Console.WriteLine("Query Array:-->");
foreach (var item in queryvar)
{
Console.WriteLine(item);
}
}
public void Exercise2() //string array
{
string[] names = { "Aryaman", "Bhaavik", "Bhavin", "Chitaksh", "Daksh", "Darshit", "Devansh", "Dhanuk", "Dhairya", "Divij", "Divyansh", "Ehsaan", "Eeshan", "Faiyaz", "Farhan" };
IEnumerable<string> query1 = from n in names
where n.Contains("an")
select n;
Console.WriteLine("query1:-->");
foreach (var q in query1)
{
Console.WriteLine(q);
}
Console.WriteLine("query2:-->");
IEnumerable<string> query2 = from n in names
where n.StartsWith("Dh")
select n;
foreach (var q in query2)
{
Console.WriteLine(q);
}
Console.WriteLine("query3:-->");
IEnumerable<string> query3 = from n in names
where n.EndsWith("h")
select n;
foreach (var q in query3)
{
Console.WriteLine(q);
}
}
public void Exercise3()
{
Student s1 = new Student() { Id = 1, Name = "Ajay", Age = 29, Gender = 'M' };
Student s2 = new Student() { Id = 2, Name = "Rina", Age = 21, Gender = 'F' };
Student s3 = new Student() { Id = 3, Name = "Ajeet", Age = 25, Gender = 'M' };
Student s4 = new Student() { Id = 4, Name = "Alok", Age = 21, Gender = 'M' };
Student s5 = new Student() { Id = 5, Name = "Pankaj", Age = 27, Gender = 'M' };
Student s6 = new Student() { Id = 6, Name = "Kamni", Age = 21, Gender = 'F' };
Student s7 = new Student() { Id = 7, Name = "Priya", Age = 23, Gender = 'F' };
List<Student> students = new List<Student>();
students.Add(s1);
students.Add(s2);
students.Add(s3);
students.Add(s4);
students.Add(s5);
students.Add(s6);
students.Add(s7);
var querystds = from std in students
where std.Age > 25
orderby std.Name
select std;
Console.WriteLine("Query List of students:-->");
foreach (var std in querystds)
{
Console.Write("{0} {1} {2}", std.Id, std.Name, std.Age);
Console.WriteLine();
}
var groupedGender = from s in students
group s by s.Gender into GrpStudent
select new
{
Sex = GrpStudent.Key,
AverageAge = Math.Round(GrpStudent.Average(g => g.Age), 2)
} ;
foreach (var gg in groupedGender)
{
Console.WriteLine(gg);
}
Console.WriteLine("GROUPED DATA:");
var grps = from s in students
group s by s.Gender;
foreach (var grp in grps)
{
foreach (var gr in grp)
{
Console.WriteLine("{0} {1} {2} {3}",gr.Id,gr.Name,gr.Age,gr.Gender);
}
}
Console.WriteLine("SORTED GROUPED DATA:");
var sortgrps = from s in students
orderby s.Gender
group s by s.Gender;
foreach (var grp in sortgrps)
{
foreach (var gr in grp)
{
Console.WriteLine("{0} {1} {2} {3}", gr.Id, gr.Name, gr.Age, gr.Gender);
}
}
Console.WriteLine("STATISTICAL FIGURES");
var statgrps = from s in students
orderby s.Gender
group s by s.Gender;
foreach (var gr in statgrps)
{
Console.WriteLine("Sum of {0}: {1}", gr.Key, gr.Sum(s => s.Age));
Console.WriteLine("Max of {0}: {1}", gr.Key, gr.Max(s => s.Age));
Console.WriteLine("Min of {0}: {1}", gr.Key, gr.Min(s => s.Age));
Console.WriteLine("Average of {0}: {1}", gr.Key, Math.Round(gr.Average(s => s.Age),2));
Console.WriteLine("Count of {0}: {1}",gr.Key ,gr.Count());
Console.WriteLine("Count of {0} whose age is 21: {1}",gr.Key ,gr.Count(s=>s.Age==21));
Console.WriteLine();
}
}
public void Exercise4()
{
Console.WriteLine("Illustration of deferred execution");
int[] data = { 9, 2, 1, 8, 4, 6, 0 };
List<int> even = new List<int>();
for (int i = 0; i < data.Length; i++)
{
if (data[i] % 2 == 0)
{
even.Add(data[i]);
}
}
data[2] = 6;
//display even
foreach (var e in even)
{
Console.Write(e.ToString() +" ");
}
int[] data2 = { 9, 2, 1, 8, 4, 6, 0 };
IEnumerable<int> queryvar = from r in data2
where r % 2 == 0
select r;
data2[2] = 6;
//display query result
//execute query using for each loop
// query deffered until foreach loop used
Console.WriteLine();
foreach (var e in queryvar)
{
Console.Write(e.ToString() + " ");
}
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Exercise1();
t.Exercise2();
t.Exercise3();
t.Exercise4();
Console.ReadKey();
}
}
}
OUTPUT
Query Array:-->
8
6
query1:-->
Aryaman
Devansh
Dhanuk
Divyansh
Ehsaan
Eeshan
Farhan
query2:-->
Dhanuk
Dhairya
query3:-->
Chitaksh
Daksh
Devansh
Divyansh
Query List of students:-->
1 Ajay 29
5 Pankaj 27
{ Sex = M, AverageAge = 25.5 }
{ Sex = F, AverageAge = 21.67 }
GROUPED DATA:
1 Ajay 29 M
3 Ajeet 25 M
4 Alok 21 M
5 Pankaj 27 M
2 Rina 21 F
6 Kamni 21 F
7 Priya 23 F
SORTED GROUPED DATA:
2 Rina 21 F
6 Kamni 21 F
7 Priya 23 F
1 Ajay 29 M
3 Ajeet 25 M
4 Alok 21 M
5 Pankaj 27 M
STATISTICAL FIGURES
Sum of F: 65
Max of F: 23
Min of F: 21
Average of F: 21.67
Count of F: 3
Count of F whose age is 21: 2
Sum of M: 102
Max of M: 29
Min of M: 21
Average of M: 25.5
Count of M: 4
Count of M whose age is 21: 1
Illustration of deferred execution
2 8 4 6 0
2 6 8 4 6 0