Usage of C# (C Sharp) Query Keywords – select, from, where, in

This 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 {
static void Main() {
int[] sampleArray = { 11, 50, 45, 80, 68, 29};
int tempVar;
Console.WriteLine(“Array values that are even (except 80) are mentioned below:”);
for (int index=0; index<sampleArray.length; index++) {
tempVar = sampleArray[index];
if(tempVar %2 == 0 && tempVar != 80) {
Console.Write(tempVar + ", ");
}
}
}
}

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 {
static void Main() {
int[] sampleArray = { 11, 50, 45, 80, 68, 29};
var queryResult = from arrayValue in sampleArray
where arrayValue%2 == 0 && arrayValue != 80
select arrayValue;
Console.WriteLine(“Array values that are even (except 80) are mentioned below:”);
foreach (int resultVal in queryResult)
{
Console.Write(resultVal + ", ");
}
}
}

Output of both the above mentioned examples will be:
Array values that are even (except 80) are mentioned below:

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 {
static bool compute(int val) {
bool returnVal = false;
if(val %2 == 0 && val != 80) {
returnVal = true;
}
return returnVal;
}
static void Main() {
int[] sampleArray = { 11, 50, 45, 80, 68, 29};
var queryResult = from arrayValue in sampleArray
where compute(arrayValue)
select arrayValue;
Console.WriteLine(“Array values that are even (except 80) are mentioned below:”);
foreach (int resultVal in queryResult)
{
Console.Write(resultVal + ", ");
}
}
}

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 {
static void Main( ){
int[] array1 = { 1, 2, 3 };
int[] array2 = { 4, 5, 6 };
int[] array3 = { 7, 8, 9};
var query = from val1 in array1
from val2 in array2
from val3 in array3
select new { val1, val2, val3 };
Console.WriteLine(“Permutation of elements belonging to all three arrays are:”);
foreach (var result in query){
Console.WriteLine ("({0},{1},{2})", result.val1, result.val2, result.val3);
}
}
}

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.

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.