Usage of C# (C Sharp) Query Keywords select, from, where, inThis article will help you in understanding the usage of keywords select, from and where in framing LINQ Query Expression.
When you are working with database, you will write many SQL queries. One example for a simplest query is shown below: select id, name from employee where employee.id > 10; This example is very much confined to querying database. What if you can use similar statements to query on other data sources like arrays and classes? Hope it sounds interesting. C# provides select, from, where clause in the form of keywords serving similar purpose as that of database queries. Assume that you have an integer array called sampleArray. You have to display only elements containing even numbers other than 80. How will you do it in your normal C# coding? Here is the answer: class sampleClass
{ You can implement the same logic with the help of LINQ query expression using select, from, where query keywords. Equivalent coding for the above mentioned code is given below: class sampleClass
{ Output of
both the above mentioned examples will be: 50, 68 The second example frames the query in queryResult. The query is actually executed when you iterate it using the foreach statement. Here queryResult is of type IEnumerable<int>. Your query starts with from clause followed by where clause and then select clause. You can specify any number of conditions in the where clause. The conditions can be related using && or || operators as per your need. Not just conditions, you can even include any function calls in the where clause. Consider the following example: class sampleClass
{ This example also results in the same output. Only difference is in the where clause. You have a method call in where clause instead of specifying the conditions. This method is defined in the same class. In the example you are looking at, the conditions are pretty simple and straight forward. But there are situations when you have to perform complicated logic based on which the query has to be executed. In such cases, you can perform the logic inside a method and call that method in the where clause of the query. But ensure that the method should always return Boolean value only. While framing the query, you can position your where clause anywhere other than the first and last position. In the above example, from clause is in first position, select clause is in the last position and where clause is positioned in between. Hence it is perfectly legal. Specifying where clause in the query is optional. The examples discussed above have only one from clause included. But you can include any number of from clauses in the same query. Here is a simple example containing three from clauses: class sampleClass
{ This example performs the permutation of each element of array1 with other than arrays. Output of this code will contain combinations like (1, 4, 7) (1,4,8) (1,4,9) (1,5,7) and so on. In all these examples you can note the usage of in keyword. It is used in two different places, in the query to fetch each of the array elements and in the foreach statement to fetch one result of the query at a time. From these examples, it is clear that select clause is used to specify what all values have to be displayed during query execution. In one of the examples, simple select statement shown below is used: select arrayValue; And another example specified three elements one from each array: select new { val1, val2, val3 }; Not just these two, select clauses are used to perform many different computations and their corresponding results. Ensure that your query should either end up using select clause or using group clause.
|