• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Linq
 

Linq

on

  • 3,015 views

An introduction on LINQ

An introduction on LINQ

Statistics

Views

Total Views
3,015
Views on SlideShare
2,486
Embed Views
529

Actions

Likes
4
Downloads
0
Comments
2

3 Embeds 529

http://at-agent.dev 515
http://192.168.1.70 13
http://192.168.1.50 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • not nice we cannot save why you deploy ???? for you yes for you
    Are you sure you want to
    Your message goes here
    Processing…
  • nice
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Th
  • Language Integrated Query ( LINQ , pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages using a syntax reminiscent of SQL. Many of the concepts that LINQ has introduced were originally tested in Microsoft's Cω research project. LINQ was released as a part of .NET Framework 3.5 on November 19, 2007. LINQ is available for any .NET languages. Through visual Studio you can find support for C#, VB.NET. LINQ is a uniform programming model for any kind of data it introduces queries as first class concept in any .NET languages.
  • You can write LINQ queries against various enumerable data sources (that is, a data source that implements the IEnumerable interface). Examples are in-memory data structures (Collections, Lists, etc.,), XML documents, SQL databases, and  DataSet objects. LINQ to SQ L: Allows to querying a relational structure by converting LINQ query into a native SQL Query. LINQ to Dataset : Interacts with data already in ADO.NET DataSet structures. LINQ to Entity : To query a logical model that usually has a higher-level representation of data information compared to direct access to relational data sources. When LINQ query operates on data that is on relational database, the LINQ query generates an equivalent SQL statements.
  • The above query can be applied to SQL Database , DataSet, or an array of objects in memory or many other kinds of data. Here Customer could be a collection of objects. The SQL like syntax in LINQ called as query expressions . The reason to start with a from clause instead of a select statement, as in SQL syntax, is related to the need to provide Microsoft IntelliSense capabilities within the remaining part of the query, which makes writing conditions, selections, and any other LINQ query clauses easier.
  • At the core, LINQ works over anything that implements IEnumerable . Natively includes a number of lists that are part of the .NET Framework as well as lists you create. It also works with lists of elements and attributes present in XML documents as well as fields and tables present in DataSets. By extending the base IEnumerable with a new interface, IQueryable , we can add a layer which retains the query structure and has the ability to translate it into a language more native to the underlying data source (like TSQL for SQL Server). Out of the box, the .NET Framework ships with two such providers: LINQ to SQL and LINQ to Entities.
  • The where query operator defines the restriction to filter the collection. The Orderxx operators are used for sorting. ThenBy and ThenByDescending operators are used for a secondary sort if the first sort gives similar result.
  • The where query operator defines the restriction to filter the collection. The Orderxx operators are used for sorting. ThereBy and ThereByDescending operators are used for a secondary sort if the first sort gives similar result.
  • Add some more stuff on Extending LINQ (CVM)

Linq Linq Presentation Transcript

  • Language Integrated Query Ch. Vishwa Mohan Freelance Software Consultant & Corporate Trainer
  • Table of Contents
    • Introducing LINQ
    • New Language Enhancements in C# 3.0
    • Language Fundamentals
    • Advanced Query Operators in LINQ
    • LINQ to ADO.NET
    • LINQ to XML
    • The Entity Framework
  • Introducing LINQ
    • What is LINQ ?
      • Stands for Language INtegrated Query .
      • LINQ makes it possible to access different data source with the same syntax.
      • It is a part of Microsoft .NET Framework 3.5 component.
      • It adds native data querying capabilities to .NET language using a syntax reminiscent of SQL.
      • Allows developers to query data structures using SQL-like syntax from within their applications code.
      • LINQ is another tool for embedding SQL queries into code.
      • The key feature of LINQ is its integration with widely used programming language, mad possible by the usage of syntax common to all kind of content.
    • LINQ provides consistent model for working with data across various kinds of data sources and formats.
      • In a LINQ query, you are always working with objects.
      • You 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 Basics
    • LINQ is a unified programming model for any kind of data. It can allow the developers to query and manage sequence of items ( Eg: In memory collections, tables in database, XML Nodes , and so on ).
      • LINQ enable you to query and manipulate data with a consistent model that is independent from data source (May be SQL Server, XML file, Arrays or Lists, Text file, e-mail message, SOAP message, etc., ).
      • SQL Like syntax used in LINQ is called “ query expressions ”.
    • LINQ queries are expressed in the programming language itself, and not as string literals embedded in the application code. So it offers several advantages:
      • Eliminates the need of using separate language.
      • With Visual Studio 2008 IDE you will also benefit compile time checking, static typing and Intellisense support.
    • LINQ to ADO.NET will implements the functionality needs to manipulate the relational data. It has the following modules:
      • LINQ to SQL, LINQ to DataSet and LINQ to Entity
  • LINQ Basics
  • LINQ Basics
    • The LINQ family of technologies provides a consistent query experience for
      • objects ( LINQ ),
      • relational databases ( LINQ to SQL )
      • XML ( LINQ to XML ).
  • Introducing LINQ
    • All LINQ query operations consists of three distinct operations:
      • Obtain the data source :
      • int[] mynumbers = new int[] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 };
        • A LINQ data source is any object that supports the generic IEnumerable<T> interface, or an interface that inherits from it.
        • Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source. ( But there is small change in query )
        • var oddNumbers = from int num in mynumbers ..,//specify num type as int.
      • Create the query :
      • var oddNumbers = from num in mynumbers
      • where (num % 2) != 0
      • select num;
        • The from clause specifies the data source, the where clause applies the filter, and the select clause specifies the type of the returned elements.
      • // oddNumbers is an IEnumerable<int> . So you can use foreach on this.
      • Execute the query :
      • foreach (int num in oddNumbers) {Console.Write(&quot;{0,1} &quot;, num); }
  • Introducing LINQ
  • LINQ Introduction
    • LINQ query variables are typed as IEnumerable<T> or a derived type such as IQueryable<T> . Let us see the below query
    • var customerQuery = from cust in customers
    • where cust.City == &quot;London&quot;
    • select cust;
      • Here var is a type of IEnumerable<Customer> . It means that when the query is executed it will produce sequence of zero or more Customer objects.
    • Types that support IEnumerable<T> or a derived interface such as IQueryable<T> are called queryable types . You can create LINQ expression on these types.
      • The types that implements IEnumerable ( Non generic version ) interface also be used as a LINQ data source. (Eg: ArrayList ).
      • A queryable type requires no modification or special treatment to serve as a LINQ data source.
      • If the source data is not already in memory as a queryable type, the LINQ provider must represent it as such.
      • Eg: LINQ to XML loads an XML document into a queryable  XElement  type: 
  • LINQ – Language INtegrated Query
  • LINQ Syntax var customerQuery = from cust in customers where cust.City == &quot;London&quot; select cust;
  • LINQ Operators Classification
  • Standard Query Operators in LINQ
    • LINQ query defines a declarative syntax for the most common operators. They are categorized as follow:
      • Restriction Operators: where
        • The where operator defines the restriction to filter the collection.
      • Project Operators: select, selectMany
        • These select and selectMany operators are defines a projection to select values of the result based on a selector function.
      • Ordering Operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse.
        • The OrderBy used to ascending sorting and OrderByDescending used for descending sorting.
        • The ThenBy and TheyByDescending operators are used for a secondary sort if the first sort gives a similar result.
        • Reverse reverses the elements in a collection.
        • In LINQ query expression, you can add several values separated by comma to the orderby expression. It takes the OrderBy() for first element and ThenBy() for the elements that follows.
    • var myStudents = from s in students where s.Marks >80 orderby s.Marks , s.Age select s;
  • Standard Query Operators in LINQ
    • JOIN Related Operators: Join, GroupJoin
      • With the Join operator a join of two collections can be done. This is similar to JOIN in SQL.
    • Grouping Operators: GroupBy
      • This operator groups a collection with a common key.
    • Quantifiers: Any, All, Contains
      • Any operator defines if any element in the collection satisfies a predicate function.
      • All determines if all elements in the collection satisfy a predicate.
      • Contains checks whether specific element is in the collection.
    • Partitioning Operators: Take, Skip, TakeWhile, SkipWhile .
      • With these operators you will get a partial result. With Take , you have to specify the number of elements to take from the collection, whereas the Skip ignores the specified number of elements and takes remaining.
      • The TakeWhile/SkipWhile takes/skips the elements as long as a condition is true.
  • Standard Query Operators in LINQ
    • Set Operators: Distinct, Union, Intersect, Except
      • All these operators returns a collection set. The Distinct operator removes duplications from a collection. The Union operator returns unique elements that appear in the given two collections. The Intersect return elements that appear in both collections. The Except returns elements that appear in just one collection.
    • Element Operators : First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, ElementAt, ElementAtDefault .
      • All these operators just returns one element.
      • The First operator returns first element that satisfies the condition.
      • The FirstOrDefault is similar to First , but it returns a default value of the type if the element is not found.
      • Similarly, Last returns the last element that satisfies the condition.
      • With ElementAt operator you can query for specific position element to return.
      • The Single returns the only one element that satisfies the condition. If more then one element satisfies the condition an exception is thrown.
    • Aggregate Operators: Count, Sum, Min, Max, Average, Aggregate
  • Standard Query Operators in LINQ
    • Conversion Operators: ToArray, ToEnumerable, ToList, ToDictionary OfType<T>
      • These operators converts the collections to an Array, IEnumerable, IList , etc.,
    • Generation Operators: Empty, Range, Repeat
      • These operators returns a new collection. With the Empty operator the empty collection is returned. The Range operator returns sequence of numbers and Repeat returns a collection with one repeated value.
    • Miscellaneous Operators: Concat, EqualAll
    • Custom Sequence Operators: Combine
    • Query Execution: Deferred, Immediate, QueryReuse
  • Extending LINQ
    • In Visual Studio 2008 includes LINQ provider assemblies that enable the use of LINQ with
      • .NET Framework Collections
      • SQL Server Databases
      • ADO.NET Datasets
      • XML Documents.
    • LINQ can be extended to support potentially any kind of data store. LINQ provider are being written by third parties for many web services and other database implementations.
    • Microsoft’s LINQ extensions are:
      • LINQ to SQL
      • LINQ to DataSet
      • LINQ to Entities
      • LINQ to XML
  • LINQ Project
    • You can create any project on .NET Framework version 3.5 supports LINQ.
    • To enable basic LINQ functionality in your application :
      • Just reference to System.Core.dll.
      • Add using directive statement for System.Linq to your source code file or project.
      • To enable advanced LINQ functionality such as expression trees you need add using directive statement for System.Linq.Expressions .
      • To enable LINQ to XML in your file additionally add using directive for System.Xml.Linq .
      • To enable LINQ to SQL in your file
        • Add reference to System.Data.Linq.dll & System.Data.dll
        • Add the System.Data.Linq namespace reference with using directive.
      • To enable LINQ to DataSet in your file additionally
        • Add reference to System.Data.DataSetExtensions.dll & System.Data.dll
        • Add the System.Data.Linq namespace reference.
  • Visual Studio IDE Support for LINQ.
    • The Visual Studio 2008 IDE provides the following features that support LINQ application development:
      • Object Relational Designer :
        • It is a visual design tool that you can use in LINQ to SQL applications to generate classes in C# or VB that represent the relational data in an underlying database.
      • SQLMetal Command Line Tool:
        • It is a command-line tool that can be used in build processes to generate classes from existing databases for use in LINQ to SQL applications
      • LINQ-Aware Code Editors :
        • Both the C# and Visual Basic code editors support LINQ extensively with new IntelliSense and formatting capabilities.
      • Debugger Support:
        • The Visual Studio debugger supports debugging of query expressions.
  • Thank You !