[HowTo] - Exploring the LINQ Universe (VB.Net)

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

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

Introduction

Lets recap the Example of LINQ from [HowTo] - First Start with LINQ (VB.Net).

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

Solutions

Essential:
grafik

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

Query Syntax:

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

The availability of LINQ

In nontechnical words the availability of LINQ depends on the datatype of a variable. A particular datatype exposes LINQ functionalities when a set of extension capabilities is present.

Also it is recommended to have the System.Linq namespace added to the imports
grafik

a more technical summary

LINQ capabilities are available when a set of extension methods on the IEnumerable<T> interface is implemented and exposed.

LINQ Offerings

In general LINQ is available on the datatype for following sources:

  • LINQ To Objects: Array, List, Dictionary, String, DataTable
  • LINQ To Data: within the context of DataSet, SQL, Entity (Entity Framework)
  • LINQ to XML
  • Custom Sources: JSON offered by the Newtonsoft API

Following checks are intended to give some starting help and ommit the more technical specifics and variants.

Check 1: direct availability

In case of uncertainty the automplete feature (IntelliSense) can be used for checking if LINQ related extension methods are presented.
The typical methods to check are: All, Any, Where, Select

grafik

Check 2: availability over Cast()

In case of the above mentioned methods are not offered following check can be done:

  • is there an GetEnumerator method?
  • is there a Cast method?
    In a lot of cases LINQ methods can be made avaialble by using the Cast method on first

grafik

Examples

grafik

Check 3: AsEnumerable method, Datatables

LINQ can be used on a variable after transforming it into a collection of datatype EnumerableXXX<T>

grafik

The capabilities of LINQ

Following table gives a short overview on the LINQ standard operators (LINQ methods)

Group Operators
Restriction (Filtering) Where
Projection (Selection) Select, SelectMany
Ordering OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending
Quantifiers All, Any, Contains
Join Join (inner join), GroupJoin (left outer join)
Grouping GroupBy
Aggregation Aggregate, Average, Count, LongCount, Max, Min, Sum
Partitioning (Paging) Skip, SkipWhile, Take, TakeWhile
Set Distinct, Except, Intersect, Union
Conversion AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup
Element Access ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
Generation DefaultIfEmpty, Empty, Range, Repeat
Other Concat, SequenceEqual, Zip

References

Questions

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

8 Likes