LINQ Architecture & Overview

Page 1

Linq iFour Consultancy

https://www.ifourtechnolab.com/


Architecture ďƒ˜LINQ has a 3-layered architecture in which the uppermost layer consists of the language extensions and the bottom layer consists of data sources that are typically objects implementing IEnumerable<T> or IQueryable<T> generic interfaces

https://www.ifourtechnolab.com/


Architecture (cont)  Language Integrated Query

• An innovation introduced in Visual Studio 2008 and .NET Framework version 3.5 that bridges the

gap between the world of objects and the world of data • Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support • LINQ makes a query a first-class language construct in C# and Visual Basic. Write queries against strongly typed collections of objects by using language keywords and familiar operators

https://www.ifourtechnolab.com/


Architecture (cont) ďƒ˜ Linq to Object

• It deals with in-memory data. Any class that implements the IEnumerable interface can be queried

with Standard Query Operators

Example: string[] greetings = { "Hello world", "Hello LINQ", "Hello Apress"}; var items = from s in greetings where s.EndsWith("LINQ") select s; foreach (var item in items) txtResult.Text += item + "n"; Result => Hello LINQ

https://www.ifourtechnolab.com/


Architecture (cont) ďƒ˜ Linq to XML

• This provider converts an XML document to a collection of XElement objects, which are then

queried against using the local execution engine that is provided as a part of the implementation of the standard query operator Example: var titles = from book in books.Elements("book") where (string)book.Element("author") == "Steve Nolle" select book.Element("title"); foreach (var title in titles) txtResult.Text = title.Value; Result => Software Engineering https://www.ifourtechnolab.com/


Architecture (cont)  Linq to ADO.NET

• Deals data from external sources, basically anything ADO.NET can connect to. Any class that

implements Ienumerable or IQueryable can be queried with Standard Query Operators • The LINQ to ADO.NET functionality could be achieved by using System.Data.Linq namespace Example: DataClasses1DataContext dc1 = new DataClasses1DataContext(); var hotel = from h in dc1.tbl_Hotels where h.city == "London" select h.hotelName; foreach (string h in hotel) txtResult.Text += h + "/n";

https://www.ifourtechnolab.com/


Advantages of Linq  Offers Intelligence which means writing more accurate queries easily  Writing codes is quite faster in LINQ and thus development time also gets reduced

significantly  Viewing relationship between two tables is easy with LINQ due to its hierarchical feature and this enables composing queries joining multiple tables in less time  It is extensible that means it is possible to use knowledge of LINQ to querying new data source types  Offers the facility of joining several data sources in a single query as well as breaking complex problems into a set of short queries easy to debug  Offers easy transformation for conversion of one data type to another like transforming SQL data to XML data.

https://www.ifourtechnolab.com/


IEnumerable Vs IQueryable IEnumerable

IQueryable

Namespace

System.Collections Namespace

System.Linq Namespace

Derives from

No base interface

Derives from IEnumerable

How does it work

While querying data from database, IEnumerable executes select query on server side, load data inmemory on client side and then filter data. Hence does more work and becomes slow.

While querying data from database, IQueryable executes select query on server side with all filters. Hence does less work and becomes fast.

Suitable for

LINQ to Object and LINQ to XML queries

LINQ to SQL queries

Custom Query

Doesn’t support

Supports using CreateQuery and Execute methods

Extension method parameter

Extension methods supported in IEnumerable takes functional objects.

Extension methods supported in IEnumerable takes expression objects, i.e. expression tree

When to use

When querying data from in-memory collections like List, Array, etc

When querying data from out-memory (like remote database, service) collections.

Best Uses

In-memory traversal

Paging

https://www.ifourtechnolab.com/


Query Operators  List Of Operators • • • • • • • • • • • • •

Filtering Operators Join Operators Projection Operations Sorting Operators Grouping Operators Concatenation Aggregation Quantifier Operations Partition Operations Generation Operations Set Operations Equality Element Operators

=> => => => => => => => => => => => =>

where() join(), groupjoin() select(), selectmany() orderby(), orderbydescending(), thenby(), thenbydescending(), Reverse() groupby() concat() aggregate(), average(), count(), longcount(), max(), Min(), sum() all(), any(), contains() skip(), Skipwhile(), Take(), Takewhile() defaultIfEmpty(), Empty(), Range(), Repeat() Distinct(), Except(), Intersect(), Union() sequenceEqual() ElementAt(), ElementAtOrDefault(), First(), FirstorDefault(), Last(), LastorDefault(), Single(), SingleorDefault(), DefaultIfEmpty()

https://www.ifourtechnolab.com/


Query Operators  Where()

: Filter values based on a predicate function db.myTable.Where(x => x.column=”------”).select(x=>x.column)  Join() : The operator join two sequences on basis of matching keys db.table1.Join(db.table2,x => x.ID, y=> y.Post_ID,(x, y) => new { Post = x, Meta = y}).Where(z => z.Post.ID == id);  GroupJoin() : Join two sequences and group the matching elements db.table1.GroupJoin(table2,c => c.Code,o => o.KeyCode,(c, result) => new Result(c.Name, result));  Select() : The operator projects values on basis of a transform function db.table1.select(c=>c.column)  Selectmany() : The operator project the sequences of values which are based on a transform function as well as flattens them into a single sequence people.SelectMany(p => p.PhoneNumbers,(parent, child) => new { parent.Name, child.Number });  Orderby() : Sorting by column l

https://www.ifourtechnolab.com/


References  https://msdn.microsoft.com/en-us/library/bb397906.aspx  https://msdn.microsoft.com/en-us/library/bb308959.aspx  https://www.tutorialspoint.com/linq/linq_overview.htm  http://www.tutorialspoint.com/linq/

https://www.ifourtechnolab.com/


Thank You.

https://www.ifourtechnolab.com/


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.