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).
- int[] marks = { 55, 34, 91, 63, 29, 88, 16, 35, 72, 90 };
- var fail =
- from n in marks
- where n < 35
- select n;
- Console.WriteLine("marks of the student who faild in this exam:");
- foreach (var x in fail)
- {
- Console.WriteLine(x);
- }
- Console.ReadLine();
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.
- class Program
- {
- static void Main(string[] args)
- {
- example ex = new example();
- ex.example1();
- }
-
- public class student
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string address { get; set; }
- public string phNo { get; set; }
- public int marks { get; set; }
- }
- public class example
- {
- private List<student> studentList;
- public List<student> getStudentList()
- {
- if (studentList == null)
- createList();
- return studentList;
- }
- public void example1()
- {
- List<student> st = getStudentList();
- var stud = from x in st
- where x.marks < 50
-
- select x;
- Console.WriteLine("all students have marks less then 50");
- foreach( var a in stud)
- {
- Console.WriteLine(" student {0} :{1}", a.ID, a.Name);
- }
- Console.ReadLine();
- }
- private void createList()
- {
- studentList = (from e in XDocument.Load(@"D:\StudentRecords.xml").Root.Elements("student")
- select new student
- {
- ID = (int)e.Element("id"),
- address = (string)e.Element("address"),
- Name = (string)e.Element("name"),
- marks = (int)e.Element("marks"),
- phNo = (string)e.Element("PhNo")
- }).ToList();
- }
- }
- }
Example 3
In this example we will see an AND operator used in a where clause.
- List<student> stud = getStudentList();
- var query =
- from st in stud
- where st.marks > 80 && st.Name =="Rajeev"
- select st;
- Console.WriteLine("Example of AND in where clause");
- foreach (var x in query)
- {
- Console.WriteLine("{0} marks obtained by {1}", x.marks,x.Name);
- }
- Console.ReadLine();
Example 4
In this example we will see an indexed where clause returning digits whose name is shorter than their value.
- string[] digits = { "a", "Ranjan", "Manish", "Prerana", "Nisa", "Ashok", "Santosh", "Rajeev", "Izlyn", "Meera" };
- var adv = digits.Where((digit, index) => digit.Length < index);
- Console.WriteLine("Short digits:");
- foreach (var d in adv)
- {
- Console.WriteLine("The word {0} is shorter than its value. And his length is :- {1}", d,d.Length);
- }
- Console.ReadLine();
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.
- int[] no = { 66, 44, 71, 83, 99, 83, 65, 77, 22, 60 };
- var x =
- from n in no
- select n + 1;
- Console.WriteLine("Numbers + 1:");
- foreach (var i in x)
- {
- Console.WriteLine(i);
- }
- Console.ReadLine();
Example 2
In this example we will see how to only get some fields rather then all table columns.
- List<student> st = getStudentList();
- var stud = from x in st
- select x.Name;
- Console.WriteLine("All Students Names ");
- foreach (var a in stud)
- {
- Console.WriteLine(a);
-
- }
- Console.ReadLine();
Example 3
In this example we will see a sequence of strings representing the text version of a sequence of ints.
- int[] no = { 1, 4, 3, 2, 9, 5, 6, 7, 8, 0 };
- string[] str = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
- var query =
- from n in no
- select str[n];
- Console.WriteLine("Inputted text value in string formate ");
- foreach (var s in query)
- {
- Console.WriteLine(s);
- }
- Console.ReadLine();
Example 4
In this example we will see how to change the case.
- string[] words = { "Rajeev", "RaNjAn", "PrErANa" };
- var query =
- from w in words
- select new { Upper = w.ToUpper(), Lower = w.ToLower() };
- foreach (var ul in query)
- {
- Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower);
- }
- Console.ReadLine();
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.