Practical approach to Language Integrated Query (LINQ) 26/06/2009 HEALTHCARE Vaidhyanathan R Kaaleeswari K vaidhya.r@tcs.com kaaleeswari.k@tcs.com TCS Public Practical approach to Language Integrated Query (LINQ) Introduction Language-Integrated Query (LINQ) is a new feature introduced in visual studio 2008 and NET Framework 3.5 Developers can write the queries in the code to retrieve the data from various types of data sources like SQL, XML and XQuery Earlier developers have to learn the syntax for writing queries for each data source LINQ simplifies by offering a model to work with the data across various kinds of data sources LINQ Architecture Internal Use Practical approach to Language Integrated Query (LINQ) Language Features that Enable LINQ LINQ makes heavy use of Generics Additionally, there were a number of features added to the Visual Basic and C# languages specifically to support LINQ The following contains a partial list of the language features that help enable LINQ and a brief description of each: • • • • • • Type Inference: Shorthand indicating the variables type is the compile time type of the right hand assignment Extension methods: Extending an existing value or reference type without deriving a new type Object Initializer: Short form of object initialization syntax that generates the equivalent code Anonymous types: Create statements without constructing a method or type Lambda expressions: Concise way of creating inline methods Query expressions: SQL-like statements within code for manipulating objects Internal Use Practical approach to Language Integrated Query (LINQ) Flavors of LINQ There are a variety of flavors of LINQ for accessing and manipulating different data sources The trailing list contains some of the data domains provided by Microsoft Some of these will be topics of future NET Nuts and Bolts articles • • • • • LINQ to Objects: Manipulates collections of objects LINQ to DataSets: Manipulates a DataSet using LINQ LINQ to SQL: Maps between custom types and a physical database table schema LINQ to Entities: Uses a conceptual Entity Data Model to create a conceptual model of a physical database LINQ to XML: Allows querying and manipulation of XML Internal Use Practical approach to Language Integrated Query (LINQ) Introduction to LINQ Syntax LINQ Query operation contains three parts Obtain the data source Create the Query Execute the Query LINQ Example class LINQExample { static void Main() { // The Three Parts of a LINQ Query: // Data source string[] names = new string[4] { “TOM”, “DAN”, “ADAMS”, “BERNARD” }; // Query creation // nameQuery is an IEnumerable var nameQuery = from name in names where name == “TOM” select name; // Query execution foreach (int name in nameQuery) { Console.Write("{0,1} ", name); } } } The following table outlines some of the options available with LINQ syntax Destination var = Using type inference to assign the resulting value(s) Source from in Information source providing a set of item(s) Filter where , distinct Expression specifying the selection criteria Order order by , Control the ordering of [Ascending | Descending] the results Aggregate count([]), sum(), min(), max(), avg() Aggregate the source items Projection select Shaping the output Internal Use Practical approach to Language Integrated Query (LINQ) Extracting data from existing XML file Add the following code to the Page_Load event and run the application XDocument xmlDoc = XDocument.Load(@"C:\Documents and Settings\164164\My Documents\Visual Studio 2008\Backup Files\SampleWebforXML\SampleWebforXML\sample.xml"); var q = from c in xmlDoc.Descendants("employee") where (int)c.FirstAttribute == select c.Element("name").Value; foreach (var p in q) Response.Write(p); Internal Use 19 Practical approach to Language Integrated Query (LINQ) References 1) 2) 3) 4) 5) 6) http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx Head First C# book http://www.codeproject.com/KB/linq/LINQtoXML.aspx http://dotnetslackers.com/articles/csharp/IntroducingLINQ1.aspx http://www.codeguru.com/csharp/csharp/net30/article.php/c13715 http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part1.aspx Internal Use 20 ... • • LINQ to Objects: Manipulates collections of objects LINQ to DataSets: Manipulates a DataSet using LINQ LINQ to SQL: Maps between custom types and a physical database table schema LINQ to Entities:... Model to create a conceptual model of a physical database LINQ to XML: Allows querying and manipulation of XML Internal Use Practical approach to Language Integrated Query (LINQ) Introduction to LINQ... model to work with the data across various kinds of data sources LINQ Architecture Internal Use Practical approach to Language Integrated Query (LINQ) Language Features that Enable LINQ LINQ makes