You are on page 1of 46

Example: LINQ Query Syntax in C#

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// string collection

IList<string> stringList = new List<string>() {

"C# Tutorials",

"VB.NET Tutorials",

"Learn C++",

"MVC Tutorials" ,

"Java"

};

// LINQ Query Syntax

var result = from s in stringList

where s.Contains("Tutorials")

select s;

foreach (var str in result)

Console.WriteLine(str);

}
C# Tutorials
VB.NET Tutorials
MVC Tutorials

Example: LINQ Query Syntax in C#


using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

// LINQ Query Syntax to find out teenager students

var teenAgerStudent = from s in studentList

where s.Age > 12 && s.Age < 20

select s;

Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudent){


Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Example: LINQ Method Syntax in C#

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// string collection

IList<string> stringList = new List<string>() {

"C# Tutorials",
"VB.NET Tutorials",

"Learn C++",

"MVC Tutorials" ,

"Java"

};

// LINQ Method Syntax

var result = stringList.Where(s => s.Contains("Tutorials"));

foreach (var str in result)

Console.WriteLine(str);

}
C# Tutorials
VB.NET Tutorials
MVC Tutorials

Example: Method Syntax in C#

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

{
// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

// LINQ Query Method to find out teenager students

var teenAgerStudent = studentList.Where(s => s.Age > 12 && s.Age < 20);

Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudent){

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Anonymous method in C#:


using System;

public class Program

delegate bool IsTeenAger(Student stud);

public static void Main()

IsTeenAger isTeenAger = delegate(Student s) { return s.Age > 12 && s.Age < 20; };

Student stud = new Student() { Age = 25 };

Console.WriteLine(isTeenAger(stud));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
False
using System;

public class Program

delegate bool IsTeenAger(Student stud);

public static void Main()

IsTeenAger isTeenAger = s => s.Age > 12 && s.Age < 20;

Student stud = new Student() { Age = 25 };

Console.WriteLine(isTeenAger(stud));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
False

Example: Specify multiple parameters in lambda expression C#


using System;

public class Program

delegate bool IsYoungerThan(Student stud, int youngAge);

public static void Main()

IsYoungerThan isYoungerThan = (s, youngAge) => s.Age < youngAge;

Student stud = new Student() { Age = 25 };

Console.WriteLine(isYoungerThan(stud, 26));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
True

example: Lambda expression with zero parameter.


using System;

public class Program


{

delegate void Print();

public static void Main()

Print print = () => Console.WriteLine("This is parameter less lambda expression");

print();

}
This is parameter less lambda expression

Multiple statements in body expression:


using System;

public class Program

delegate bool IsYoungerThan(Student stud, int youngAge);

public static void Main()

IsYoungerThan isYoungerThan = (s, youngAge) => {

Console.WriteLine("Lambda expression with multiple statements in the body");

return s.Age < youngAge;

};
Student stud = new Student() { Age = 25 };

Console.WriteLine(isYoungerThan(stud, 26));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
Lambda expression with multiple statements in the body
True

Local variable in Lambda Expression body:


using System;

public class Program

delegate bool IsAdult(Student stud);

public static void Main()

IsAdult isAdult = (s) => {

int adultAge = 18;


Console.WriteLine("Lambda expression with multiple statements in the body");

return s.Age >= adultAge;

};

Student stud = new Student() { Age = 25 };

Console.WriteLine(isAdult(stud));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
Lambda expression with multiple statements in the body
True

Func Delegate:
Example: Lambda expression assigned to Func delegate in C#
using System;

public class Program

{
public static void Main()

Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;

Student stud = new Student() { Age = 21 };

Console.WriteLine(isStudentTeenAger(stud));

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
False

Action Delegate:
using System;

public class Program

public static void Main()


{

Action<Student> PrintStudentDetail = s => Console.WriteLine("Name: {0}, Age: {1} ",


s.Name, s.Age);

Student std = new Student(){ Name = "Bill", Age=21};

PrintStudentDetail(std);

public class Student{

public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}
Name: Bill, Age: 21

Example: Func delegate in LINQ Method Syntax


using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

{
// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;

var teenAgerStudent = studentList.Where(isStudentTeenAger);

Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudent){

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Example: Func delegate in LINQ Query Syntax

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;

var teenAgerStudents = from s in studentList

where isStudentTeenAger(s)

select s;
Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudents){

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Standard Query Operators in Query Syntax:

Standard Query
Operators in Quer Syntax

Standard Query Operators in Method Syntax:


Standard Query Operators in Method Syntax

Where clause in Query Syntax:


Example: Where clause - LINQ query syntax C#
using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

// LINQ Query Syntax to find out teenager students

var teenAgerStudent = from s in studentList


where s.Age > 12 && s.Age < 20

select s;

Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudent){

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Exapmle: Where clause


using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()


{

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

Func<Student,bool> isTeenAger = delegate(Student s) {

return s.Age > 12 && s.Age < 20;

};

var filteredResult = from s in studentList

where isTeenAger(s)

select s;

foreach (var std in filteredResult)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }


}

Where extension method in Method Syntax


using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

// LINQ Query Method to find out teenager students

var teenAgerStudent = studentList.Where(s => s.Age > 12 && s.Age < 20);

Console.WriteLine("Teen age Students:");

foreach(Student std in teenAgerStudent){

Console.WriteLine(std.StudentName);
}

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Teen age Students:
John
Bill
Ron

Example: Linq - Where extension method in C#


using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 13} ,

new Student() { StudentID = 2, StudentName = "Moin", Age = 21 } ,


new Student() { StudentID = 3, StudentName = "Bill", Age = 18 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }

};

var filteredResult = studentList.Where((s, i) => {

if(i % 2 == 0) // if it is even element

return true;

return false;

});

foreach (var std in filteredResult)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Multiple Where clause:
var filteredResult = from s in studentList
where s.Age > 12
where s.Age < 20
select s;

Filtering Operator - OfType


using System;

using System.Linq;

using System.Collections;

public class Program

public static void Main()

IList mixedList = new ArrayList();

mixedList.Add(0);

mixedList.Add("One");

mixedList.Add("Two");

mixedList.Add(3);

mixedList.Add(new Student() { StudentID = 1, StudentName = "Bill" });

var stringResult = from s in mixedList.OfType<string>()

select s;

var intResult = from s in mixedList.OfType<int>()

select s;

var stdResult = from s in mixedList.OfType<Student>()

select s;
foreach (var str in stringResult)

Console.WriteLine(str);

foreach (var integer in intResult)

Console.WriteLine(integer);

foreach (var std in stdResult)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
One
Two
0
3
Bill

OfType in Method Syntax:


Example: OfType in C#
var stringResult = mixedList.OfType<string>();
Sorting Operators: OrderBy &
OrderByDescending
Example: OrderBy in Query Syntax C#
using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,

new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }

};

var orderByResult = from s in studentList

orderby s.StudentName //Sorts the studentList collection in ascending order

select s;

var orderByDescendingResult = from s in studentList //Sorts the studentList collection in


descending order

orderby s.StudentName descending


select s;

Console.WriteLine("Ascending Order:");

foreach (var std in orderByResult)

Console.WriteLine(std.StudentName);

Console.WriteLine("Descending Order:");

foreach (var std in orderByDescendingResult)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Ascending Order:
Bill
John
Ram
Ron
Steve
Descending Order:
Steve
Ron
Ram
John
Bill

OrderBy in Method Syntax:


Example: OrderBy in Method Syntax C#

using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,

new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }

};

var studentsInAscOrder = studentList.OrderBy(s => s.StudentName);

var studentsInDescOrder = studentList.OrderByDescending(s => s.StudentName);


Console.WriteLine("Ascending Order:");

foreach (var std in studentsInAscOrder)

Console.WriteLine(std.StudentName);

Console.WriteLine("Descending Order:");

foreach (var std in studentsInDescOrder)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Ascending Order:
Bill
John
Ram
Ron
Steve
Descending Order:
Steve
Ron
Ram
John
Bill
OrderByDescending:
Example: OrderByDescending C#
using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,

new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }

};

var studentsInAscOrder = studentList.OrderBy(s => s.StudentName);

var studentsInDescOrder = studentList.OrderByDescending(s => s.StudentName);

Console.WriteLine("Ascending Order:");

foreach (var std in studentsInAscOrder)


Console.WriteLine(std.StudentName);

Console.WriteLine("Descending Order:");

foreach (var std in studentsInDescOrder)

Console.WriteLine(std.StudentName);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

Multiple Sorting:
Example: Multiple sorting in Query syntax C#
using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()


{

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,

new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }

};

var multiSortingResult = from s in studentList

orderby s.StudentName, s.Age

select s;

foreach (var std in multiSortingResult)

Console.WriteLine("Name: {0}, Age {1}",std.StudentName, std.Age);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ron, Age 19
Name: Steve, Age 15

Sorting Operators: ThenBy &


ThenByDescending
Example: ThenBy & ThenByDescending
using System;

using System.Linq;

using System.Collections.Generic;

public class Program

public static void Main()

// Student collection

IList<Student> studentList = new List<Student>() {

new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,

new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } ,

new Student() { StudentID = 3, StudentName = "Bill", Age = 25 } ,

new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,

new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }

};

var multiSortingResult = from s in studentList

orderby s.StudentName, s.Age


select s;

foreach (var std in multiSortingResult)

Console.WriteLine("Name: {0}, Age {1}",std.StudentName, std.Age);

public class Student{

public int StudentID { get; set; }

public string StudentName { get; set; }

public int Age { get; set; }

}
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ron, Age 19
Name: Steve, Age 15

Example: ThenBy & ThenByDescending VB.Net


Dim sortedResult = studentList.OrderBy(Function(s) s.StudentName)
.ThenBy(Function(s) s.Age)

Dim sortedResult = studentList.OrderBy(Function(s) s.StudentName)


.ThenByDescending(Function(s) s.Age)
Grouping Operators: GroupBy &
ToLookup
Example: GroupBy in Query syntax C#
using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName =
"John", Age = 18 } ,
new Student() { StudentID = 2, StudentName =
"Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName =
"Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName =
"Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName =
"Ron" , Age = 21 }
};

var groupedResult = from s in studentList


group s by s.Age;

//iterate each group


foreach (var ageGroup in groupedResult)
{
Console.WriteLine("Age Group: {0}", ageGroup.Key); /
/Each group has a key

foreach(Student s in ageGroup) // Each group has inn


er collection
Console.WriteLine("Student Name: {0}", s.Stud
entName);
}

public class Student{

public int StudentID { get; set; }


public string StudentName { get; set; }
public int Age { get; set; }

}
Age Group: 18
Student Name: John
Student Name: Bill
Age Group: 21
Student Name: Steve
Student Name: Ron
Age Group: 20
Student Name: Ram

GroupBy in Method Syntax:


Example: GroupBy in method syntax C#
using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName =
"John", Age = 18 } ,
new Student() { StudentID = 2, StudentName =
"Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName =
"Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName =
"Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName =
"Ron" , Age = 21 }
};

var groupedResult = studentList.GroupBy(s => s.Age);

foreach (var ageGroup in groupedResult)


{
Console.WriteLine("Age Group: {0}", ageGroup.Key);
//Each group has a key

foreach(Student s in ageGroup) //Each group has a i


nner collection
Console.WriteLine("Student Name: {0}", s.Stud
entName);
}

public class Student{

public int StudentID { get; set; }


public string StudentName { get; set; }
public int Age { get; set; }

}
Age Group: 18
Student Name: John
Student Name: Bill
Age Group: 21
Student Name: Steve
Student Name: Ron
Age Group: 20
Student Name: Ram

ToLookup
using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName =
"John", Age = 18 } ,
new Student() { StudentID = 2, StudentName =
"Steve", Age = 21 } ,
new Student() { StudentID = 3, StudentName =
"Bill", Age = 18 } ,
new Student() { StudentID = 4, StudentName =
"Ram" , Age = 20 } ,
new Student() { StudentID = 5, StudentName =
"Ron" , Age = 21 }
};

var lookupResult = studentList.ToLookup(s => s.Age);

foreach (var group in lookupResult)


{
Console.WriteLine("Age Group: {0}", group.Key); //E
ach group has a key

foreach(Student s in group) //Each group has a inne


r collection
Console.WriteLine("Student Name: {0}", s.Stud
entName);
}

public class Student{

public int StudentID { get; set; }


public string StudentName { get; set; }
public int Age { get; set; }

}
Age Group: 18
Student Name: John
Student Name: Bill
Age Group: 21
Student Name: Steve
Student Name: Ron
Age Group: 20
Student Name: Ram

Joining Operator: Join


Example: Join operator C#
using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<string> strList1 = new List<string>() {
"One",
"Two",
"Three",
"Four"
};

IList<string> strList2 = new List<string>() {


"One",
"Two",
"Five",
"Six"
};

var innerJoinResult = strList1.Join(// outer sequence


strList2, // inner sequence
str1 => str1, // outerKeySelector
str2 => str2, // innerKeySelector
(str1, str2) => str1);

foreach (var str in innerJoinResult)


{
Console.WriteLine("{0} ", str);
}

}
}
One
Two

Example Classes
public class Student{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardID { get; set; }
}

public class Standard{


public int StandardID { get; set; }
public string StandardName { get; set; }
}

Example: Join Query C#


using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName =
"John", Age = 18, StandardID = 1 } ,
new Student() { StudentID = 2, StudentName =
"Steve", Age = 21, StandardID = 1 } ,
new Student() { StudentID = 3, StudentName =
"Bill", Age = 18, StandardID = 2 } ,
new Student() { StudentID = 4, StudentName =
"Ram" , Age = 20, StandardID = 2 } ,
new Student() { StudentID = 5, StudentName =
"Ron" , Age = 21 }
};

IList<Standard> standardList = new List<Standard>() {


new Standard(){ StandardID = 1, StandardName=
"Standard 1"},
new Standard(){ StandardID = 2, StandardName=
"Standard 2"},
new Standard(){ StandardID = 3, StandardName=
"Standard 3"}
};

var innerJoinResult = studentList.Join(// outer sequence


standardList, // inner sequence
student => student.StandardID, // outerKeySelecto
r
standard => standard.StandardID, // innerKeySelecto
r
(student, standard) => new // result selector
{
StudentName = student.StudentName,
StandardName = standard.StandardNa
me
});

foreach (var obj in innerJoinResult)


{

Console.WriteLine("{0} - {1}", obj.StudentName, obj.


StandardName);
}

public class Student{

public int StudentID { get; set; }


public string StudentName { get; set; }
public int Age { get; set; }
public int StandardID { get; set; }
}

public class Standard{


public int StandardID { get; set; }
public string StandardName { get; set; }
}
John - Standard 1
Steve - Standard 1
Bill - Standard 2
Ram - Standard

Example: Join operator in query syntax C#

using System;
using System.Linq;
using System.Collections.Generic;

public class Program


{
public static void Main()
{
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName =
"John", Age = 18, StandardID = 1 } ,
new Student() { StudentID = 2, StudentName =
"Steve", Age = 21, StandardID = 1 } ,
new Student() { StudentID = 3, StudentName =
"Bill", Age = 18, StandardID = 2 } ,
new Student() { StudentID = 4, StudentName =
"Ram" , Age = 20, StandardID = 2 } ,
new Student() { StudentID = 5, StudentName =
"Ron" , Age = 21 }
};
IList<Standard> standardList = new List<Standard>() {
new Standard(){ StandardID = 1, StandardName=
"Standard 1"},
new Standard(){ StandardID = 2, StandardName=
"Standard 2"},
new Standard(){ StandardID = 3, StandardName=
"Standard 3"}
};

var innerJoinResult = from s in studentList // outer sequen


ce
join st in standardLi
st //inner sequence
on s.StandardID equal
s st.StandardID // key selector
select new { // resul
t selector

StudentName = s.StudentName,

StandardName = st.StandardName
}
;

foreach (var obj in innerJoinResult)


{

Console.WriteLine("{0} - {1}", obj.StudentName, obj.


StandardName);
}

}
}

public class Student{

public int StudentID { get; set; }


public string StudentName { get; set; }
public int Age { get; set; }
public int StandardID { get; set; }
}

public class Standard{

public int StandardID { get; set; }


public string StandardName { get; set; }
}

John - Standard 1
Steve - Standard 1
Bill - Standard 2
Ram - Standard 2

Joining Operator: GroupJoin

Example Classes
public class Student{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardID { get; set; }
}

public class Standard{


public int StandardID { get; set; }
public string StandardName { get; set; }
}
xample: GroupJoin in Method syntax C#

You might also like