LINQ
- LINQ stands for "Language Integrated Query".
- This concept is introduced in .NET Framework 3.5.
- This is a query writing technology.
- This is most useful while working large amount of data in the live projects.
INTRODUCTION:
- In relational database system, data is organized in the form of tables, on which you can write SQL queries to retrieve the required data according to the requirement in the application.
- But you can‘t write a query on the non-database data, which in the form of objects in the application. There, you can write the queries using the new concept called "LINQ".
- You can write queries on arrays, objects, databases and XML using LINQ.
- Note: Before writing the LINQ queries, you should import the System.Linq namespace..
PROGRAM FOR LINQ TO ARRAYS
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LINQtoArrayDemo { class Program { static void Main(string[] args) { //data source int[] numbers = { 2, 12, 10, 5, 15, 4, 62 }; //ling query IEnumerableresult = from n in numbers where n <= 10 select n; //output foreach (var x in result) Console.WriteLine(x); Console.Read(); } } }
In the above application, the array contains few numbers. After executing the query, you got only the numbers, which are less than 10. In this manner, you can execute the queries on data sets (after learning ADO.NET) also.
LINQ SYNTAX:
THE ABOVE SYNTAX CONSISTS OF 7 CLAUSES.
- from clause
- in clause
- let clause
- where clause
- orderby clause
- select clause
- group by clause
- Mandatory clauses:
- from clause
- in clause
- select clause
CLAUSE
Def of Clause: A part of the query.
UNDERSTANDING CLAUSES:
- from clause: This is used to specify the iteration variable name. This acts as alias name for the data source.
- in clause: This is used to specify the main data source for the query.
- let clause (optional): This is used to declare a new identifier with a value, that is to be used during the query execution.
- where clause (optional): This is most frequently used optional clause, using which you can specify the condition in the query.
- orderby clause (optional): This is used to specify the sorting expression if required.
- select clause: This is used to specify the object, which is required in the query results.
- group by (optional): This is similar to ―group by‖ clause in SQL. This retrieves grouped data, based on a column.
Note: The result of a LINQ query should be assigned into a IEnumerable type variable. IEnumerable is an interface.
Library: System.Collections.Generic.IEnumerablePROGRAM FOR LINQ TO OBJECTS
Student.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LINQtoObjectsDemo { class Student { //fields public int StudentID; public string Name; public string Course; public int Marks; //constructor public Student(int StudentID, string Name, string Course, int Marks) { this.StudentID = StudentID; this.Name = Name; this.Course = Course; this.Marks = Marks; } } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LINQtoObjectsDemo { class Program { static void Main(string[] args) { //data source Student[] stu = { new Student(101, "Prakash", "MBA", 765), new Student(102, "Pradeep", "MBA", 471), new Student(103, "Pushpa", "Msc", 590), new Student(104, "Purna", "MCA", 223), new Student(105, "Purnima", "MCA", 450)}; //linq query with where clause IEnumerableresult1 = from s in stu where s.Course == "MCA" select s; Console.WriteLine("MCA Students:"); foreach (Student r in result1) Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); //linq query with compound where clause IEnumerable result2 = from s in stu where s.Name.EndsWith("a") && s.Marks>=400 && s.Marks<=600 select s; Console.WriteLine("\nStudents whose name ends with 'a', and marks is >=400 and <=600:"); foreach (Student r in result2) Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); //linq query with let and where clauses IEnumerable result3 = from s in stu let avg = s.Marks / 10 where avg < 35 select s; Console.WriteLine("\nFailed Students:"); foreach (Student r in result3) Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); //linq query with orderby clause IEnumerable result4 = from s in stu orderby s.Marks select s; Console.WriteLine("\nStudents (sort on marks):"); foreach (Student r in result4) Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); //linq query with orderby clause (descending) IEnumerable result5 = from s in stu orderby s.Marks descending select s; Console.WriteLine("\nStudents (sort on marks - descending):"); foreach (Student r in result5) Console.WriteLine(r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); //linq query with group clause IEnumerable > result6 = from s in stu group s by s.Course; Console.WriteLine("\nStudents with grouping:"); foreach (IGrouping StuGrp in result6) { Console.WriteLine(StuGrp.Key + ":"); foreach (Student r in StuGrp) Console.WriteLine(" " + r.StudentID + ", " + r.Name + ", " + r.Course + ", " + r.Marks); } Console.Read(); } } }
No comments:
Post a Comment