Linq Query Programs
Linq Query Programs
using System;
using System.Linq;
using System.Collections.Generic;
// string collection
"C# Tutorials",
"VB.NET Tutorials",
"Learn C++",
"MVC Tutorials" ,
"Java"
};
where s.Contains("Tutorials")
select s;
Console.WriteLine(str);
}
C# Tutorials
VB.NET Tutorials
MVC Tutorials
using System.Linq;
using System.Collections.Generic;
// Student collection
};
select s;
}
Teen age Students:
John
Bill
Ron
using System;
using System.Linq;
using System.Collections.Generic;
// string collection
"C# Tutorials",
"VB.NET Tutorials",
"Learn C++",
"MVC Tutorials" ,
"Java"
};
Console.WriteLine(str);
}
C# Tutorials
VB.NET Tutorials
MVC Tutorials
using System;
using System.Linq;
using System.Collections.Generic;
{
// Student collection
};
var teenAgerStudent = studentList.Where(s => s.Age > 12 && s.Age < 20);
Console.WriteLine(std.StudentName);
}
Teen age Students:
John
Bill
Ron
IsTeenAger isTeenAger = delegate(Student s) { return s.Age > 12 && s.Age < 20; };
Console.WriteLine(isTeenAger(stud));
}
False
using System;
Console.WriteLine(isTeenAger(stud));
}
False
Console.WriteLine(isYoungerThan(stud, 26));
}
True
print();
}
This is parameter less lambda expression
};
Student stud = new Student() { Age = 25 };
Console.WriteLine(isYoungerThan(stud, 26));
}
Lambda expression with multiple statements in the body
True
};
Console.WriteLine(isAdult(stud));
}
Lambda expression with multiple statements in the body
True
Func Delegate:
Example: Lambda expression assigned to Func delegate in C#
using System;
{
public static void Main()
Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;
Console.WriteLine(isStudentTeenAger(stud));
}
False
Action Delegate:
using System;
PrintStudentDetail(std);
}
Name: Bill, Age: 21
using System.Linq;
using System.Collections.Generic;
{
// Student collection
};
Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;
Console.WriteLine(std.StudentName);
}
Teen age Students:
John
Bill
Ron
using System;
using System.Linq;
using System.Collections.Generic;
// Student collection
};
Func<Student, bool> isStudentTeenAger = s => s.Age > 12 && s.Age < 20;
where isStudentTeenAger(s)
select s;
Console.WriteLine("Teen age Students:");
Console.WriteLine(std.StudentName);
}
Teen age Students:
John
Bill
Ron
Standard Query
Operators in Quer Syntax
using System.Linq;
using System.Collections.Generic;
// Student collection
};
select s;
Console.WriteLine(std.StudentName);
}
Teen age Students:
John
Bill
Ron
using System.Linq;
using System.Collections.Generic;
// Student collection
};
};
where isTeenAger(s)
select s;
Console.WriteLine(std.StudentName);
using System.Linq;
using System.Collections.Generic;
// Student collection
};
var teenAgerStudent = studentList.Where(s => s.Age > 12 && s.Age < 20);
Console.WriteLine(std.StudentName);
}
}
Teen age Students:
John
Bill
Ron
using System.Linq;
using System.Collections.Generic;
// Student collection
};
return true;
return false;
});
Console.WriteLine(std.StudentName);
}
Multiple Where clause:
var filteredResult = from s in studentList
where s.Age > 12
where s.Age < 20
select s;
using System.Linq;
using System.Collections;
mixedList.Add(0);
mixedList.Add("One");
mixedList.Add("Two");
mixedList.Add(3);
select s;
select s;
select s;
foreach (var str in stringResult)
Console.WriteLine(str);
Console.WriteLine(integer);
Console.WriteLine(std.StudentName);
}
One
Two
0
3
Bill
using System.Linq;
using System.Collections.Generic;
// Student collection
};
select s;
Console.WriteLine("Ascending Order:");
Console.WriteLine(std.StudentName);
Console.WriteLine("Descending Order:");
Console.WriteLine(std.StudentName);
}
Ascending Order:
Bill
John
Ram
Ron
Steve
Descending Order:
Steve
Ron
Ram
John
Bill
using System;
using System.Linq;
using System.Collections.Generic;
// Student collection
};
Console.WriteLine(std.StudentName);
Console.WriteLine("Descending Order:");
Console.WriteLine(std.StudentName);
}
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;
// Student collection
};
Console.WriteLine("Ascending Order:");
Console.WriteLine("Descending Order:");
Console.WriteLine(std.StudentName);
Multiple Sorting:
Example: Multiple sorting in Query syntax C#
using System;
using System.Linq;
using System.Collections.Generic;
// Student collection
};
select s;
}
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ron, Age 19
Name: Steve, Age 15
using System.Linq;
using System.Collections.Generic;
// Student collection
};
}
Name: Bill, Age 25
Name: John, Age 18
Name: Ram, Age 20
Name: Ron, Age 19
Name: Steve, Age 15
}
Age Group: 18
Student Name: John
Student Name: Bill
Age Group: 21
Student Name: Steve
Student Name: Ron
Age Group: 20
Student Name: Ram
}
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;
}
Age Group: 18
Student Name: John
Student Name: Bill
Age Group: 21
Student Name: Steve
Student Name: Ron
Age Group: 20
Student Name: Ram
}
}
One
Two
Example Classes
public class Student{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardID { get; set; }
}
using System;
using System.Linq;
using System.Collections.Generic;
StudentName = s.StudentName,
StandardName = st.StandardName
}
;
}
}
John - Standard 1
Steve - Standard 1
Bill - Standard 2
Ram - Standard 2
Example Classes
public class Student{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int StandardID { get; set; }
}