Current Version of this topic is DRAFT and will be finalized / enhanced soon
Introduction
Let’s 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 the number is greater than 10
Solutions
Essential:
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
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 the 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 omit the more technical specifics and variants.
Check 1: direct availability
In case of uncertainty, the autocomplete feature (IntelliSense) can be used for checking if LINQ-related extension methods are presented.
The typical methods to check are: All, Any, Where, Select
Check 2: availability over Cast()
In case of the above-mentioned methods are not offered following check can be done:
- is there a GetEnumerator method?
- is there a Cast method?
In a lot of cases, LINQ methods can be made available by using the Cast method on first
Examples
Check 3: AsEnumerable method, Datatables
LINQ can be used on a variable after transforming it into a collection of datatype EnumerableXXX<T>
Same example in Query Syntax
(From d in dtData.AsEnumerable()
Where d(2).ToString.Equals("Y")
Select r = d).CopyToDataTable
Overview LINQ capabilities - Operators
The following table gives a short overview of 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, Count, LongCount, Max, Min, Sum, Average |
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 |
Conjunction | Concat, Append, Prepend |
Generation | DefaultIfEmpty, Empty, Range, Repeat |
Other | SequenceEqual, Zip |
References
- linqsamples https://linqsamples.com
Questions
For questions on your retrieval case open a new topic and get individual support