C# – Query Expressions

Query Expressions contains a set of instructions to select the data source and retrieve the data from the selected data source.

Define a Query

C# provides clauses to define a query. A query will start using the from clause and ended using either select clause or a group clause. Where to store the query? We need to store the query in the query variable. The important thing we need to remember is the declaration type to store the query. The query variable should be either generic type or should be of IEnumerable<T> type. This allows to fetch the data when required.

And another thing to remember is, query variable contains the query ONLY; not the result of the query (data).

Here is the example to define a query:

int [] values = {1, 3, 5, 7, 9, 11, 13};
IEnumerable<int> qv = from value in values select value;

Retrieve the Data

Once the query is defined, we will have the query variable. Using the query variable we will execute the query to fetch the data. The results of the query will be stored in a different variable. For example, we can use foreach statement to fetch the data using the query defined in the query variable.

foreach (int v in qv)
Console.WriteLine(v);

We will discuss more topics in my upcoming Articles.

(Raju)

C# – Query Expressions

Leave a Reply

Your email address will not be published.

Scroll to top