120 likes | 157 Views
LINQ makes a query a first-class language construct in C# and Visual Basic. 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.<br>
E N D
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 https://www.ifourtechnolab.com/
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(), longcount(), 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() 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 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/