LINQ Quick Reference With C#
LINQ Quick Reference With C#
Abhimanyu Kumar Vatsa Microsoft & C# Corner MVP, Blogs at ITORIAN.COM Sam Hobbs Editor, C# Corner
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
This book is a basic introduction to LINQ (Language Integrated Query) basically for beginners who want to learn complete basic with example of LINQ.
Table of Contents 1. Basic Introduction to LINQ 1.1 What is LINQ 1.2 Benefits of LINQ 1.3 Alder Approach. 2. Examples related to LINQ 2.1 LINQ to Array 2.2 LINQ to XML. 3. Generic Collections in LINQ 4. Demo Project explores LINQ queries 5. Selecting Data using LINQ 6. Query Operations in LINQ 6.1 Filter 6.2 Order 6.3 Group 6.4 Join 7. Joining Multiple Data-Sources using Concate Key in LINQ 8. Customizing LINQs Select Statement 9. Transforming Data source objects into XML using LINQ 10. Performing Calculation in LINQ 11. LINQ Query Syntax and Method Syntax
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
This is not only huge code, but there's also no way for the C# compiler to check our query against our use of the data it returns. When "e" retrieves the employee's name we have to know the column's position in the database table to find it in the result. It's a common mistake to retrieve the wrong column and get a type exception or bad data at run time.
But ?
With LINQ you just need to perform three distinct actions: 1. 2. 3. Obtain the data source. Create the query. Execute the query.
If you execute the above program, you will get "2 4 6 8 10" as the output. You will notice 3 actions above, these actions will always be changed depending upon our project requirements. In the above example, the "data source" was an array that implicitly supports the generic IEnumerable<T> interface.
2.2 LINQ to XML 2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Now, let's move on to take a look at a LINQ to XML example and find student names from an XML file. LINQ to XML loads an XML document into a query-able XElement type and then IEnumerable<XElement> loads the query results, and using a foreach loop, we access it. Student.XML File
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Very simple examples on LINQ so far, stay tuned, you will find more stuff.
Collection Types
Advantages
Arrays
Type Safe like arrays Can grow automatically like ArrayList Convenient to work with Add, Remove
Now, let's discuss each of the collection types given in the above table to feel the real pain of development.
Array
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
If you use array: You can't enter more values beyond the initialized size otherwise you will see a runtime error saying "index was outside the bounds of the array". If you declare an array as an int type then you are limited to enter integer values only otherwise you will see a compile time error saying "cannot implicitly convert type string to int", all because the array is type-safe. The array is an index based, so you always need to write index number to insert value in the collection. You aren't adding and remove methods here.
ArrayList(System.Collection)
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
10
List<T>(System.Collection.Generic)
Now, in short we can say, a list has both the features that we have in arrays and arraylists.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
11
In the above example, we have used the IEnumerable<T> interface and a LINQ query and then by using a foreach loop I'm able to get my data. One more example: IEnumerable<Customer> customerQuery = from cust in customers where cust.City == "Bokaro" select cust;
foreach (Customer customer in customerQuery) { Console.WriteLine(customer.LastName + " = " + customer.Age); } In the above example you can see, using a LINQ query I'm not only limited to select the string value that is LastName, I'm also able to select an integer value that is Age. So, this could be the great example of generic classes. Here is some counterpart of Generic Collections over non-generic collections:
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
12
Step 1: Creating the New Project 2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Open the Visual Studio instance and create a new project New > Project > Console Application and name the project whatever you wish.
13
This will add all required libraries as well as classes to your project for database communication. Actually, this DBML file will become a layer between the DB and your project and provide you all the IntelliSense features.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
14
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Once you are done with the above, drag the tables from the "Server Explorer" to the "DataNorthwind.dbml" file designer; see:
15
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
16
You will notice the following things here: 1. 2. A new connection string in the App.config file. Every setup required to communicate with the DB like DataNorthwindDataContext, TableAttribute, EntitySet etc in was created in the DataNorthwind.designer.cs file.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
17
We already saw this code in one of the previous articles. Now, we have a demo project setup completely and ready to try all the LINQ queries. In an upcoming article, we will look at various LINQ queries and will test it using the preceding project.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
18
In the above image, you can see, I'm using the same queries two times, but there is a difference, don't go on output screen only. If you look at the code carefully, you will find I'm using IEnumerable<Employee> in the first block of code and just a "var" in the second block of code. Also note, for IEnumerable<Employee> I'm using "Employee" in the foreach loop and for "var" I'm using "var" in the foreach loop, you should care it always. Now, what is the difference between "IEnumerable<Employee>" and "var"? IEnumerable<Employee>: When you see a query variable that is typed as IEnumerable<T>, it just means that the query, when it is executed, will produce a sequence of zero or more T objects, as given in the following image (just hover the mouse over "empQuery1" when the compiler is on break). When you expand the index[], you will find all the data associated with that row/record.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
If you prefer, you can avoid generic syntax like IEnumerable<T> by using the var keyword. The var keyword instructs the compiler to infer the type of a query variable by looking at the data source specified in the from clause, no big difference; you still get all the benefits. Now, let's discuss the query part. = from emp in NDC.Employees select emp; In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the "from" clause is first, to introduce the data source (NDC.Employees) and the range variable (emp). NDC is just an instance of the "Northwind Data Context" that we have created in Part 4 and inside this Data Context, we got an Employees table and that is why I have used "NDC.Employees".
19
Since we have many rows and columns in "empQuery1" or "empQuery2" returned by the LINQ select statement, we need to specify which column we want to see in the output and that's why I have used e.FirstName and e.LastName, where "e" is the instance of "empQuery1" or "empQuery2". Alternatively, you can bind "empQuery1" or "empQuery2" directly to any Data Control like GridView.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
where cust.Country == "France" select cust; foreach (var e in custQuery) { Console.WriteLine("Country: " + e.Country + " || Address: " + e.Address + " || Phone: " + e.Phone); } Console.ReadKey(); In the above query, I'm asking for only those records who's "Country" is "France". And in the foreach loop, "Country", "Address" and "Phone" are separated by "||" and the same in output.
20
In the same way, if you want to select records where "Country" is "France" and "ContactName" starts with "A", then use: var custQuery = from cust in NDC.Customers where cust.Country == "France" && cust.ContactName.StartsWith("a") select cust; And, if you want to select records where "Country" is "France" or "ContactName" starts with "A", then use: var custQuery = from cust in NDC.Customers where cust.Country == "France" || cust.ContactName.StartsWith("a") select cust; So, in both queries, "&&" is being used for "And" and "||" is being used for "Or". Now, "StartsWith" is a LINQ level key that is equivalent to the LIKE operator in SQL. You can see it in a generated query here:
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
21
6.2 Order
The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example the following query can be extended to sort the results based on the ContactName property. Because ContactName is a string, the default comparer performs an alphabetical sort from A to Z. var custQuery = from cust in NDC.Customers orderby cust.ContactName descending //orderby cust.ContactName ascending select cust;
6.3 Group
The group clause enables you to group your results based on a key that you specify. For example you could specify that the results should be grouped by the City. var custQuery = from cust in NDC.Customers where cust.ContactName.StartsWith("a") group cust by cust.City; When you end a query with a group clause, your results take the form of a list of lists. Each element in the list is an object that has a Key member and a list of elements that are grouped under that key. When you iterate over a query that produces a sequence of groups, you must use a nested foreach loop. The outer loop iterates over each group, and the inner loop iterates over each group's members. foreach (var e in custQuery) { int x = e.Key.Length; Console.WriteLine('\n'); Console.WriteLine(e.Key); Console.WriteLine(Repeat('-', x)); foreach (Customer c in e) { Console.WriteLine("Contact Name : " + c.ContactName); } } And the output will be organized as:
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
22
If you must refer to the results of a group operation, you can use the "into" keyword to create an identifier that can be queried further. The following query returns only those groups that contain more than two customers: var custQuery = from cust in NDC.Customers group cust by cust.City into custGroup where custGroup.Count() > 2 select custGroup;
And the foreach loop will be the same; see: foreach (var e in custQuery) { int x = e.Key.Length; Console.WriteLine('\n'); Console.WriteLine(e.Key); Console.WriteLine(Repeat('-', x));
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
foreach (Customer c in e) { Console.WriteLine("Contact Name : " + c.ContactName); } } You will get the following output:
23
Join
Join operations create associations among sequences that are not explicitly modeled in the data sources. For example you can perform a join to find all the customers and distributors who have the same location. In LINQ the join clause always works against object collections instead of database tables directly.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Question: What query should we write to select names from the two tables "Customer" and "Employee" depending upon matching city? Answer: The query will be: var custQuery = from cust in NDC.Customers join emp in NDC.Employees on cust.City equals emp.City select new { CityName = cust.City, CustomerName = cust.ContactName, EmployeeName = emp.FirstName }; And in the foreach loop, we will write: foreach (var e in custQuery) { Console.WriteLine(e.CityName + " : " + e.CustomerName + ", " + e.EmployeeName); } Output:
24
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Student.cs class Student { public int ID { get; set; } public string Name { get; set; } public string Address { get; set; } public List<int> Marks { get; set; } }
25
Teacher.cs class Teacher { public int ID { get; set; } public string Name { get; set; } public string Address { get; set; } }
Program.cs //DataSource1 List<Student> students = new List<Student>() { new Student { ID=1, Name="Abhimanyu K Vatsa", Address="Bokaro", Marks= new List<int> {97, 92, 81, 90}}, new Student { ID=2, Name="Deepak Kumar", Address="Dhanbad", Marks= new List<int> {70, 56, 87, 69}}, new Student { ID=3, Name="Mohit Kumar", Address="Dhanbad", Marks= new List<int> {78, 76, 81, 56}}, new Student { ID=4, Name="Geeta K", Address="Bokaro", Marks= new List<int> {95, 81, 54, 67}} }; //DataSource2 List<Teacher> teachers = new List<Teacher>() { new Teacher {ID=1, Name="Ranjeet Kumar", Address = "Bokaro"}, new Teacher {ID=2, Name="Gopal Chandra", Address = "Dhanbad"} }; And I want to select those Student's names and Teachers from "Bokaro" by Concat. So, we can use a LINQ query to create an output sequence that contains elements from more than one input sequence. Here it is: //Query using Concat var query = (from student in students where student.Address == "Bokaro" select student.Name) .Concat(from teacher in teachers where teacher.Address == "Bokaro"
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
26
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Output: Abhimanyu K Vatsa Deepak Kumar Mohit Kumar Geeta K Study 2 Now, if you want to select the name and address of the student then use the following LINQ query: var query = from student in students select new { Name = student.Name, Address = student.Address};
27
foreach (var e in query) { Console.WriteLine(e.Name + " from " + e.Address); } Output: Abhimanyu K Vatsa from Bokaro Deepak Kumar from Dhanbad Mohit Kumar from Dhanbad Geeta K from Bokaro Study 3 Now, if you want to select name, marks and even want to add it then use the following query: var query = from student in students select new { Name = student.Name, Mark = student.Marks}; int sum = 0; foreach (var e in query) { Console.Write(e.Name + " : "); foreach (var m in e.Mark) { sum = sum + m; Console.Write(m + " "); }
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Console.WriteLine(); } Output: Abhimanyu K Vatsa : 97 92 81 90 : Total- 360 Deepak Kumar : 70 56 87 69 : Total- 642 Mohit Kumar : 78 76 81 56 : Total- 933 Geeta K : 95 81 54 67 : Total- 1230 In the above query, we have the Mark field that will produce a list and that's why we need to use a nested foreach loop to get the list item and at the same time we are adding mark items.
28
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
string XMLFileInformation = "<?xml version=\"1.0\"?>\n" + studentsToXML; Console.WriteLine(XMLFileInformation); Output on Console: <?xml version="1.0"?> <School> <Student> <ID>1</ID> <Name>Abhimanyu K Vatsa</Name> <Address>Bokaro</Address> <Marks>97,92,81,90</Marks> </Student> <Student> <ID>2</ID> <Name>Deepak Kumar</Name> <Address>Dhanbad</Address> <Marks>70,56,87,69</Marks> </Student> <Student> <ID>3</ID> <Name>Mohit Kumar</Name> <Address>Dhanbad</Address> <Marks>78,76,81,56</Marks> </Student> <Student> <ID>4</ID> <Name>Geeta K</Name> <Address>Bokaro</Address> <Marks>95,81,54,67</Marks> </Student> </School> If you want to build a real XML file then use the following line: File.AppendAllText("C:\\Database.xml", XMLFileInformation); Remember to use the "System.IO" namespace to create the XML file on disk. Now, open the C drive and file a new XML file named Database.xml. In the same manner you can do this for outer data sources (not in-memory) too.
29
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
30
And we want to determine the Product's Total Price by multiplying UnitPrice and UnitInStock data. So, what would be the LINQ query to evaluate the data? Let's write a query to solve this: var query = from std in DNDC.Products where std.SupplierID == 1 select new { ProductName = std.ProductName, ProductTotalPrice = String.Format("Total Price = {0}", (std.UnitPrice) * (std.UnitsInStock)) }; To understand it, look at the preceding underlined query, you will see that a multiplication is performed on the two columns UnitPrice and UnitInStock. Once we get the result it will be converted to a string and then it will be assigned to the ProductTotalPrice variable. So, we have ProductName and ProductTotalPrice in the query that will be executed in a foreach loop to get the entire data as given below: foreach (var q in query) { Console.WriteLine("Product Name: " + q.ProductName + ", Total Price: " + q.ProductTotalPrice + ""); } When you execute above loop, you will get the following output: Product Name: Chai, Total Price: Total Price = 702.000000 Product Name: Chang, Total Price: Total Price = 323.000000 Product Name: Aniseed Syrup, Total Price: Total Price = 130.000000 So, using this way you can find the calculated information from a LINQ query too.
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
If you ask me, I recommend Query Syntax because it is usually simpler and more readable; however there is no semantic difference between Method Syntax and Query Syntax. In addition, some queries, such as those that retrieve the number of elements that match a specified condition, or that retrieve the element that has the maximum value in a source sequence, can only be expressed as method calls. The reference documentation for the standard query operators in the System.Linq namespace generally uses Method Syntax. Therefore, even when getting started writing LINQ queries, it is useful to be familiar with the use of Method Syntax in queries and in query expressions themselves. [Source: MSDN] Let's look at the example, which will produce the same result using Query Syntax and Method Syntax. int[] numbers = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; var query1 = from num in numbers where (num % 2) == 0 select num; var query2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n); Console.WriteLine("Query Syntax"); foreach (int i in query1) { Console.Write(i + " "); } Console.WriteLine("\nMethod Syntax"); foreach (int i in query2) { Console.Write(i + " "); } If you run the above code, you will get the following output: Query Syntax 2 4 6 8 10 Method Syntax 2 4 6 8 10 In query2, I use Method Syntax and you can see it also produced the same output. You can also see I'm using where and orderby clauses like a method and in the method I'm using Lambda syntaxes.
31
2013 C# CORNER. SHARE THIS DOCUMENT AS IT IS. PLEASE DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.