13
iFour Consultancy Linq

Linq by asp.net MVC development company in india

Embed Size (px)

Citation preview

Page 1: Linq by asp.net MVC development company in india

iFour Consultancy

Linq

Page 2: Linq by asp.net MVC development company in india

Architecture of LINQAdvantages of LINQIEnumerable vs IqueryableQuery Operators

INDEX

http://www.ifourtechnolab.com/

Page 3: Linq by asp.net MVC development company in india

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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 4: Linq by asp.net MVC development company in india

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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 5: Linq by asp.net MVC development company in india

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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 6: Linq by asp.net MVC development company in india

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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 7: Linq by asp.net MVC development company in india

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";

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 8: Linq by asp.net MVC development company in india

Advantages of Linq

Offers Intelligence which means writing more accurate queries easilyWriting codes is quite faster in LINQ and thus development time also gets reduced

significantlyViewing relationship between two tables is easy with LINQ due to its hierarchical feature

and this enables composing queries joining multiple tables in less timeIt is extensible that means it is possible to use knowledge of LINQ to querying new data

source typesOffers 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 debugOffers easy transformation for conversion of one data type to another like transforming

SQL data to XML data.

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 9: Linq by asp.net MVC development company in india

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 in-memory 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 methodparameter

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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 10: Linq by asp.net MVC development company in india

Query Operators

List Of Operators• Filtering Operators => where()• Join Operators => join(), groupjoin()• Projection Operations => select(), selectmany()• Sorting Operators => orderby(), orderbydescending(), thenby(), thenbydescending(), Reverse()• Grouping Operators => groupby()• Concatenation => concat()• Aggregation => aggregate(), average(), count(), loncount(), max(), Min(), sum()• Quantifier Operations => all(), any(), contains()• Partition Operations => skip(), Skipwhile(), Take(), Takewhile()• Generation Operations => defaultIfEmpty(), Empty(), Range(), Repeat()• Set Operations => Distinct(), Except(), Intersect(), Union() • Equality => sequenceEqual()• Element Operators => ElementAt(), ElementAtOrDefault(), First(), FirstorDefault(), Last(), LastorDefault(),

Single(), SingleorDefault(), DefaultIfEmpty()

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 11: Linq by asp.net MVC development company in india

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 keysdb.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 elementsdb.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 functiondb.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

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 12: Linq by asp.net MVC development company in india

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/

References

C# Software Development Companies India http://www.ifourtechnolab.com/

Page 13: Linq by asp.net MVC development company in india

Questions?

http://www.ifourtechnolab.com/