[HowTo] - First Start with LINQ (VB.Net)

LINQ is part of the .NET framework and offers capabilities for interacting with data, objects or other sources within a particular syntax. This HowTo introduces on starting with LINQ (Language-Integrated Query).

Current Version of this topic is DRAFT and will be finalized / enhanced soon

Introduction

Typical tasks for implementing parts of an automation project are interacting with e.g. with data in a datatable or items from collections. LINQ can help to implement it into a compact and standarized way. We will enter very quickly to programming and defer theoretical background topics to later.

Sample data:
myNumbers | List (Of Int32) = {12,34,5,8,10,2,15,7}.toList

Assignment:
Find all numbers in myNumbers where number is greater then 10

A typical Implementation with essential activities

  • for each# | item | Values: myNumbers | TypeArgument: int32
    • If#: item > 10
      • Then: Add to Collection# | item | Collection: myNumbersFiltered | TypeArgument: int32
Visuals

grafik

LINQ offers two syntax types and example from above can be implemented as following:

Method Syntax:
myNumbersFiltered | List (Of Int32) = myNumbers.Where(Function (x) x > 10).toList

Visuals

grafik

Query Syntax:

myNumbersFiltered | List (Of Int32) = 
(From x in myNumbers
Where x > 10
Select x).toList
Visuals

grafik

The Method Syntax:

following main building blocks are used:

myNumbersFiltered | List (Of Int32) = myNumbers.Where(Function (x) x > 10).toList

Item Comment
myNumber the item offering the LINQ capabilities
Where the LINQ Extension function (also called operator)
Function (x) do Something with x the expression statement ( so called Lambda Expression)
toList the result finalization

The Query Syntax:

following main building blocks are used:

myNumbersFiltered | List (Of Int32) =     
(From x in myNumbers
Where x > 10
Select x).toList
Item Comment
From x in XX Loop over all items from XX, the current looped item is referenced with x
Where a query operator - where for filtering
Select another query operator - select for projection - pickup x
toList the result finalization

On the first look the query syntax looks like SQL.
LINQ Syntax is NOT SQL syntax and is different.
But different query languagues (LINQ, SQL, XQuery …) do have the same purpose similarities can be found on structure and syntax.

A figuratively LINQ Processing Model

A LINQ statement can be interpretated as a pipeline with certain processing steps. This abstracted working model helps for understanding.

Step / Event Comment
START The List myNumbers contains many Int32 items
LOOP all items will be forwarded to the next step
OPERATOR 1 / EXTENSION Function works within its nature with the items
(LAMBDA) EXPRESSION takes the input and returns an output Function (x) x > 10 - Input: x (in the list example the current looped int32 item) Output: True or False
OPERATOR 1 Result x when it passed the filter or nothing
QUERY FINAL RESULT the outcome from the last processing step

Following this thinking model
myListDemo = (new List(Of Int32) From {12,2}).Where(Function (x) x > 10 ).toList
can be interpretated as

Step / Event Details
START 12,2,24
LOOP 12
WHERE In:12 Lambda 12 > 10 Out: True # Operator result: 12 as Int32
TOLIST Notice to add 12 to list
LOOP 2
WHERE In:2 Lambda 2 > 10 Out: False # Operator result: nothing
TOLIST Nothing to add
LOOP 24
WHERE In:12 Lambda 24 > 10 Out: True
TOLIST Notice to add 24 to list
QUERY FINAL RESULT List (Of int32) with items: 12,24

Internally the technical processing is more complex with some more other details, but it helps for starting with LINQ

Datatypes during processing

Lets assume following LINQ:

(From x in {12,2,24}
Where x > 10
Select x).toList

The select statement explicit projects x (an Int32 item) forwards it to ToList. A List(Of Int32) is returned.

Lets change the LINQ to following:

(From x in {12,2,24}
Where x > 10
Select x.toString()).toList

Statement iterates over all items, filters out items greater then 10 and projects it as string to a list

Visuals

grafik

The processing can be interpretated as following:

Step / Event Details
START 12,2,24
LOOP 12
WHERE In:12 Lambda 12 > 10 Out: True # Operator result: 12 as Int32
SELECT In:12 Lambda 12.toString() Out:“12” # Operator result: 12 as String
TOLIST Notice to add “12” to list
LOOP 2
WHERE In:2 Lambda 2 > 10 Out: False # Operator result: nothing
TOLIST Nothing to add
LOOP 24
WHERE In:12 Lambda 24 > 10 Out: True # Operator result: 24 as Int32
SELECT In:24 Lambda 24.toString() Out:“24”
TOLIST Notice to add “24” to list
QUERY FINAL RESULT List (Of String) with items: “12”,“24”

Refering to the processing from above, following can be concluded:

  • The Where operator expects an boolean outcome from the lambda expression
  • The Select operator expects an outcome from the lambda expression, datatype is as defined in the the lambda, returned datatypes has to be consistent
  • The Datatype for a particular operator is as given from the previous step

Outlook

In a next tutorial we will have a closer look

  • on the processing model by writing more LINQ statements
  • elaborate more on lambda expressions / Query syntax

References

Recommendations

  • Training courses from Academy
  • Debugging course from Academy (Highly recommended)

Questions

For questions on your retrieval case open a new topic and get individual support

13 Likes

Please continue your work. It is wonderful. Very nicely explained especially the ‘Processing Model’. The best resource to learn LINQ. Waiting for next tutorial.

Thanks

superb