LINQ (Language Integrated Query) is uniform
query syntax used to save and retrieve data from different sources. It
provides a single querying interface for different types of data sources. For
example, SQL is a Structured Query Language used to save and retrieve data from
a database. In the same way, LINQ is a structured query syntax used to save and
retrieve data from different types of data sources like an Object Collection,
SQL server database, XML, web service etc. LINQ always works with objects so
you can use the same basic coding patterns to query and transform data in XML
documents, SQL databases, ADO.NET Datasets, .NET collections, and any other
format for which a LINQ provider is available.
LINQ is
nothing but the collection of methods for interfaces with
methods AsIEnumerable and AsIQueryable. Linq interfaces can be defined in
CNClrLib.Linq namespaces and Dynamic Linq
interfaces are defined in CNClrLib.DynamicLinq namespace. LINQ has
two main interfaces: Theses are:
Enumerable Interface
Enumerable interface includes methods for interfaces that has AsGenericIEnumerable method, this include all the generic collection types such as GenericList interface, GenericDictionary interface, GenericSortedList interface, GenericQueue interface, GenericHashSet interface, GenericLinkedList interface etc. The following demonstrates how to use Enumerable interface methods such as First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtOrDefault, Count, Contains, Sum, Min, Max. The other methods such as Select, where etc which accepts a generic predicate can only be used if the predicate method is defined in a .net assembly. In future version, you will be able to create GenericPredicate interface from a delphi function pointer. In the meantime there is an alternative solution which is to use Dynamic Linq. The next Linq interface (Queryable Interface) explains how to use the dynamic Linq methods.
Queryable Interface
The Queryable interface includes methods for interfaces that has AsGenericIQueryable method. GenericIQueryable interface is used to provide querying capabilities against a specific data source where the type of the data is known. For example, Entity Framework api interfaces has AsGenericIQueryable method to support LINQ queries with underlaying database like SQL Server. The following demonstrates how to use dynamic Queryable interface methods such as First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtOrDefault, Count, Contains, Sum, Min, Max .
Standard Query Operators in Enumerable and Queryable
Interfaces
Classification |
Standard
Query Operators |
Filtering |
Where,
OfType |
Sorting |
OrderBy,
OrderByDescending, ThenBy, ThenByDescending, Reverse |
Grouping
|
GroupBy,
ToLookup |
Join |
GroupJoin,
Join |
Projection
|
Select,
SelectMany |
Aggregation |
Aggregate,
Average, Count, LongCount, Max, Min, Sum |
Quantifiers
|
All, Any,
Contains |
Elements |
ElementAt,
ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single,
SingleOrDefault |
Set |
Distinct, Except,
Intersect, Union |
Partitioning |
Skip, SkipWhile,
Take, TakeWhile |
Concatenation
|
Concat |
Equality
|
SequenceEqual |
Generation |
DefaultEmpty,
Empty, Range, Repeat |
Conversion |
AsEnumerable,
AsQueryable, Cast, ToArray, ToDictionary, ToList
|
Lamdba Expression