Restriction and Projection Operator in LINQ

Introduction

In this article we will learn about the Restriction and Projection operators in LINQ, their syntax and their functionality with examples. The Restriction operator is the same as a WHERE clause in SQL and Projection Operator is the same as SELECT in SQL. The Restriction operator has some limitations and some advantages. As we understand the where clause we used to filter the rows as needed. The Projection operator is used to select the records from the database.

The following are the topics that will be covered in this article:

  • Restriction Operator
  • Projection Operator

Restriction Operator

A restriction operator in LINQ specifies the statement should only affect rows that meet a specified criteria. The criteria are expressed as predicates, the where clause is not a mandatory clause of LINQ statements, but can be used to limit the number of records affected by a LINQ, the where clause is only used to extract records from select, delete, update and so on. Let's see some examples of the where clause.

Example 1

In this example we will see how to extract (filter) records using a simple condition. In this example we have a student marks list and we fetch only those marks with less than 35 marks (< 35).

 

  1. int[] marks = { 55, 34, 91, 63, 29, 88, 16, 35, 72, 90 };  
  2. var fail =  
  3.       from n in marks  
  4.       where n < 35  
  5.       select n;  
  6. Console.WriteLine("marks of the student who faild in this exam:");  
  7.   foreach (var x in fail)  
  8.  {  
  9.     Console.WriteLine(x);  
  10.  }  
  11. Console.ReadLine();  
example1

Example 2

In this example we are fetching data from an external database (from XML file). We make an XML file that stores some data and fetch that data into a list then we filter that data and display it in a console application.

  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5.     example ex = new example();  
  6.     ex.example1();  
  7. }  
  8.   
  9. public class student  
  10. {  
  11.     public int ID { getset; }  
  12.     public string Name { getset; }  
  13.     public string address { getset; }  
  14.     public string phNo { getset; }  
  15.     public int marks { getset; }  
  16. }  
  17. public class example  
  18. {  
  19.     private List<student> studentList;  
  20.     public List<student> getStudentList()  
  21.     {  
  22.         if (studentList == null)  
  23.         createList();  
  24.         return studentList;  
  25.     }  
  26. public void example1()  
  27. {  
  28.     List<student> st = getStudentList();  
  29.     var stud = from x in st  
  30.                where x.marks < 50  
  31.   
  32.                select x;  
  33.      Console.WriteLine("all students have marks less then 50");  
  34.      foreach( var a in stud)  
  35.      {  
  36.           Console.WriteLine(" student {0} :{1}", a.ID, a.Name);  
  37.      }  
  38. Console.ReadLine();  
  39. }  
  40. private void createList()  
  41. {  
  42.     studentList = (from e in XDocument.Load(@"D:\StudentRecords.xml").Root.Elements("student")  
  43.     select new student  
  44.     {  
  45.         ID = (int)e.Element("id"),  
  46.         address = (string)e.Element("address"),  
  47.         Name = (string)e.Element("name"),  
  48.         marks = (int)e.Element("marks"),  
  49.         phNo = (string)e.Element("PhNo")  
  50.     }).ToList();  
  51. }  
  52. }  
  53. }  

example1_1

Example 3

In this example we will see an AND operator used in a where clause.

  1. List<student> stud = getStudentList();  
  2. var query =  
  3.             from st in stud  
  4.             where st.marks > 80 && st.Name =="Rajeev"  
  5.             select st;  
  6. Console.WriteLine("Example of AND in where clause");  
  7. foreach (var x in query)  
  8. {  
  9.     Console.WriteLine("{0} marks obtained by {1}", x.marks,x.Name);  
  10. }  
  11. Console.ReadLine();  
example2

Example 4

In this example we will see an indexed where clause returning digits whose name is shorter than their value.

  1. string[] digits = { "a""Ranjan""Manish""Prerana""Nisa""Ashok""Santosh""Rajeev""Izlyn""Meera" };  
  2. var adv = digits.Where((digit, index) => digit.Length < index);  
  3. Console.WriteLine("Short digits:");  
  4. foreach (var d in adv)  
  5. {  
  6.     Console.WriteLine("The word {0} is shorter than its value. And his length is :- {1}", d,d.Length);  
  7. }  
  8. Console.ReadLine();  

example3

Projection Operator

The Projection operator in LINQ retrieves zero or more rows from one or more database tables. For the projection operator we use a select in LINQ and the select operator performs a projection on the collection to select elements from the database . The user supplies an arbitrary function, in the form of a lambda expression, which projects the data members. The function is passed to the operator as a delegate. Let's try to understand it with some examples.

Example 1


In this example we are trying to fetch some values and modify those values with some increments.

  1. int[] no = { 66, 44, 71, 83, 99, 83, 65, 77, 22, 60 };  
  2. var x =  
  3.       from n in no  
  4.       select n + 1;  
  5. Console.WriteLine("Numbers + 1:");  
  6. foreach (var i in x)  
  7. {  
  8.     Console.WriteLine(i);  
  9. }  
  10. Console.ReadLine();  
example4

Example 2

In this example we will see how to only get some fields rather then all table columns. 

 

  1. List<student> st = getStudentList();  
  2. var stud = from x in st  
  3. select x.Name;  
  4. Console.WriteLine("All Students Names ");  
  5. foreach (var a in stud)  
  6. {  
  7.     Console.WriteLine(a);  
  8.   
  9. }  
  10. Console.ReadLine();  
example5

Example 3

In this example we will see a sequence of strings representing the text version of a sequence of ints.

  1. int[] no = { 1, 4, 3, 2, 9, 5, 6, 7, 8, 0 };  
  2. string[] str = { "zero""one""two""three""four""five""six""seven""eight""nine" };  
  3. var query =  
  4. from n in no  
  5. select str[n];  
  6. Console.WriteLine("Inputted text value in string formate ");  
  7. foreach (var s in query)  
  8. {  
  9.     Console.WriteLine(s);  
  10. }  
  11. Console.ReadLine();  

example6

Example 4

In this example we will see how to change the case.

  1. string[] words = { "Rajeev""RaNjAn""PrErANa" };  
  2.  var query =  
  3. from w in words  
  4. select new { Upper = w.ToUpper(), Lower = w.ToLower() };  
  5. foreach (var ul in query)  
  6. {  
  7.     Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);  
  8. }
  9. Console.ReadLine();  

example7

Summary

In this article we saw two special things in LINQ , that is, the where clause and the select clause. We also saw many examples of select statements and where clauses. Thanks for reading this article.


Similar Articles