What is LINQ Language Integrated Query Make query









![LINQ to Objects C# int[] nums = new int[] {0, 4, 2, 6, 3, LINQ to Objects C# int[] nums = new int[] {0, 4, 2, 6, 3,](https://slidetodoc.com/presentation_image_h2/3d1bc984ee4b5adc827d7bbc60e2fead/image-10.jpg)



















- Slides: 29

What is LINQ? Language Integrated Query Make query a part of the language Component of. NET Framework 3. 5 Shipped with Visual Studio 2008

Linq is short for Language Integrated Query. If you are used to using SQL to query databases, you are going to have something of a head start with Linq, since they have many ideas in common.

LINQ provides a high-level abstraction of virtually any data and emulates the query operations of the relational model.

Language Integrated Query (LINQ, pronounced "link") is a Microsoft. NET Framework component that adds native data querying capabilities to. NET languages.

LINQ (often overheard pronounced as "link") is a step in the evolution of data access. It is a programming model that brings a much needed uniformity to accessing data from files, XML, database, registry, event log

LINQ Allows to perform iterations on Collections Main namespace is System. core

Query with LINQ C# var my. Customers = from c in customers where c. Region == "UK" select c;

Advantages Unified data access Single syntax to learn and remember Strongly typed Catch errors during compilation Intelli. Sense Prompt for syntax and attributes Bindable result sets More evident when more complex filters are present

Architecture C# VB. NET Others . NET Language Integrated Query (LINQ) LINQ data source providers ADO. NET support for LINQ to Objects LINQ to Datasets LINQ to SQL LINQ to Entities LINQ to XML
![LINQ to Objects C int nums new int 0 4 2 6 3 LINQ to Objects C# int[] nums = new int[] {0, 4, 2, 6, 3,](https://slidetodoc.com/presentation_image_h2/3d1bc984ee4b5adc827d7bbc60e2fead/image-10.jpg)
LINQ to Objects C# int[] nums = new int[] {0, 4, 2, 6, 3, 8, 3, 1}; double average = nums. Take(6). Average(); var above = from n in nums where n > average select n; Filtering process is easy because of the Intellisense feature

LINQ to Objects Use of Ienumerable <T> or Iqueryable<T> collection Query any IEnumerable<T> source Includes arrays, List<T>, Dictionary. . . Instead of for each loops or iterative loops the condition of LINQ used along with Ienumerable helps in rapid data retrieval

LINQ operators Aggregate Conversion Ordering Partitioning Aggregate Average Count Max Min Sum Cast Order. By Skip Of. Type Then. By Skip. While To. Array Descending Take To. Dictionar Reverse Take. While y To. List To. Lookup To. Sequenc e and many others Sets Concat Distinct Except Intersect Union

LINQ to Dataset LINQ to dataset is build on the top of ADO. Net to simplify the tasks Ensure that a reference System. Data. Set. Extensions. dll is added Typed dataset feture is present(Fields) Reshaping data with Anonymous type feature

LINQ to Entities What is the Entity Data Model? Definition for your application model Map between app model, database schema Advanced mapping scenarios supported One entity mapped across multiple tables Multiple inheritance hierarchy mappings Many-to-many without "link" table in model

LINQ to Entities or LINQ to SQL? • • • LINQ to SQL Shipped with Visual Studio 2008 and. NET 3. 5 Emphasis on rapid application development Supports Microsoft SQL Server family of databases LINQ to Entities Will ship as an update to. NET 3. 5 Offers a provider model for third-party databases Designed for enterprise-grade data scenarios Higher level of abstraction for programming databases Just one layer of the overall ADO. NET Entity Framework

LINQ to Entities: Generating Queries Converting LINQ queries to SQL Compiler converts code into a LINQ expression tree • LINQ to Entities converts LINQ expression tree into a Db. Command. Tree based on mapping information Ø Db. Command. Tree expressed in terms of the database schema • ADO. NET provider generates a Db. Command • LINQ to Entities executes the Db. Command, assembles results into the structure(s) specified in the LINQ query •

Entity Framework Query Options Entity. Client Provider string "SELECT VALUE o FROM Northwind. Entities. Orders AS o " + "WHERE o. Customers. Customer. ID = 'ALFKI'"; Entity. Command new Entity. Command(e. S Entity. Data. Reader = while ( Console. ("{0} {1: d}", ["Order. ID"], ["Order. Date"]);


LINQ to SQL Object-relational mapping Records become strongly-typed objects Data context is the controller mechanism Facilitates update, delete & insert Translates LINQ queries behind the scenes Type, parameter and injection safe

Instead of manipulating the database directly, developers manipulate the object model, which represents the database For this, Windows application must be created Add new LINQ to SQL class items to the project Drag and drop tables and make changes. It will be saved only in the object model To save changes in the database use the following syntax Database. Submitchanges();


Database mapping VS 2008 designer or SQLMetal command Map tables & fields to classes & properties Generates partial classes with attributes Each record becomes an object Data context represents the database Utilise tables, views or stored procedures

Modifying objects Update Set object properties Delete context. Table. Delete. On. Submit(object) Insert context. Table. Insert. On. Submit(object) Commit changes back context. Submit. Changes() Transactional - all or nothing

LINQ is beneficial than Stored Procedures Debugging(Complex dedug in SP) Deployment(. dll) Type Safety (Errors)

LINQ and Extension Methods Extend functionality to an existing class without needing to subclass it To add extension methods to objects implementing the Ienumerable interface, you need a reference to system. core. dll

LINQ TO XML Earlier Xpath or Xquery were used to manipulate XML documents, at present LINQ to XML concepts are used Add a reference to the System. xml. linq. dll in the project and also import System. xml. linq namespace Tree -> Xdocument ->XElement ->XAttribute LOAD XDocument Library. Books = new Xdocument(); Library. Books = Xdocument. load(“Books. xml”);

Future developments Blinq Scaffold web UI for list/view/update pages PLINQ Parallel query processing over many CPUs Sync. LINQ & Continuous LINQ Updated results via INotify. Collection. Changed

PLINQ Microsoft, as a part of the Parallel Extensions, is developing PLINQ, or Parallel LINQ, a parallel execution engine for LINQ queries. It defines the IParallel. Enumerable<T> interface. If the source collection implements this interface, the parallel execution engine is invoked. The PLINQ engine executes a query in a distributed manner on a multi-core or multiprocessor system. [28]

. NET features used. NET Framework 3. 5 Anonymous Extension Type types (shaping) methods (query operators) inference (var keyword) Lambda expressions (query syntax)