1. ASP.NET Introduction 2. Comp of .Net Framework 3.5 3. Sys.Req for VS 2008 4. ASP.NET Envrmnt Setup 5. VS2010 Ultimate Sys.Req 6. Installing VS 2010 7. VS 2012 Sys.Req 8. Installing VS Exp 2012 9. Start the VS 2008 10. Application Life Cycle 11. Page Life Cycle 12. Page Life Cycle Events 13. ASP.NET Example 14. Event Handling 15. Default Events 16. Server Side 17. Request Object 18. Response Object 19. Server Controls 20. Server Controls Properties 21. Server Controls Methods 22. HTML Server Controls 23. Client Side 24. ASP.NET Basic Controls 25. TextBox Control 26. CheckBox Controls 27. RadioButton Controls 28. ListBox Control 29. HyperLink Control 30. Image Control 31. BulletedList Control 32. ASP.NET Directives 33. Implements Directive 34. Master Type Directive 35. Page Directive 36. PreviousPage Type 37. Managing State 38. Control State 39. Cookies 40. Query Strings 41. Server-Side State 42. Session State 43. Validation Controls 44. Required FieldValidator 45. Range Validator 46. Compare Validator 47. RegularExpressionValidator 48. Custom Validator 49. Validation Summary 50. Data Source Control 51. AdRotator Control 52. Calendar Control 53. Calendar Control Example 54. Panel Control 55. Panel Control Example 56. Multi Views Control 57. MultiView & View controls 58. MultiView Control Example 59. FileUpload Control 60. FileUpload Control Eg 61. AJAX Control 62. UpdatePanel control 63. UpdateProgress Control 64. Custom Controls 65. Custom Control Eg 66. Personalization 67. Create Simple Profile 68. ADO.NET 69. ADO.NET Objects 70. DataTable 71. DataRow 72. DataColumn 73. Object Example 74. Error Handling 75. Tracing Errors 76. Debugger 77. Security 78. Authentication 79. Authorization 80. LINQ 81. LINQ Query Operators 82. LINQ Example 83. Caching 84. Data Caching 85. Output Caching 86. Object Caching 87. Web Services 88. Create Web Services Eg 89. Web & Machine.config 90. Settings Schema 91. ASP.NET Deployment 92. XCopy Deployment 93. Web Setup Project 94. ASP Interview Part 1 95. ASP Interview Part 2 96. ASP Interview Part 3 97. ASP Interview Part 4 98. ASP Interview Part 5 99. ASP Interview Part 6
Pr.Pg Next Pg

LINQ Query Operators tutorials

  • LINQ supports a large number of query operators — keywords that allow you to select, order, or filter data that is to be returned from the query. Although all of the examples in this chapter are discussed in the context of LINQ to SQL, you can easily apply them to the other LINQ implementations as well.

  • In the following section you get an overview of the most important standard query operators followed by an example. Each of the examples uses the object model and the DataContext object called myDataContext.


The Select keyword (select in C#) is used to retrieve objects from the source you are querying. In this example you see how to select an object of an existing type.


var allReviews = from r in myDataContext.Reviews select r;


The r variable in this example is referred to as a range variable that is only available within the current query. You typically introduce the range variable in the from clause, and then use it again in the Where and Select clauses to filter the data, and to indicate the data you want to select. Although you can choose any name you like, you often see single letter variables like the r (for Review) or you see the singular form of the collection you are querying (review instead of r in the preceding examples.


Order By:

With Order By (orderby in C#) you can sort the items in the result collection. Order By is followed by an optional Ascending or Descending (ascending and descending in C#) keyword to specify sort order. You can specify multiple criteria by separating them with a comma. The following query returns a list of genres first sorted by SortOrder in descending order, then sorted on their Name in ascending order (the default):


var allGenres = from g in myDataContext.Genres

orderby g.SortOrder descending, g.Name

select g;



Just like the WHERE clause in SQL, the Where clause in LINQ (where in C#) allows you to filter the objects returned by the query. The following query returns all authorized reviews:


var allReviews = from r in myDataContext.Reviews

where r.Authorized == true

select r;


Note that the Where clause uses the language’s standard equality operator: a single equals sign (=) in VB.NET and two of them in C# (= =).


Sum, Min, Max, Average, and Count:

These aggregation operators allow you to perform mathematical calculations on the objects in the result set. For example, to retrieve the average SortOrder of all genres, you can execute this query:


var average = (from g in myDataContext.Genres

select g.SortOrder).Average();


Note that the Average method is applied to the entire result set. Therefore, you need to wrap the entire statement in parentheses followed by a call to the Average method. Without the parentheses you’ll get an error. The average variable in this example will be inferred as a Double and contain the average value of all the SortOrder values in the Genre table. Not a very useful thing to calculate in a real-world application, but good enough to serve as an example of the Average method here.

var numberOfReviews = (from r in myDataContext.Reviews select r).Count();


Pr.Pg border                                              Next Pg