Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LINQ: A simple introduction

LINQ: A simple introduction

Fanie Reynders

February 16, 2013
Tweet

More Decks by Fanie Reynders

Other Decks in Programming

Transcript

  1. What is LINQ? LINQ (pronounced "link") - Language Integrated Query

    Microsoft .NET Framework component Extends powerful query capabilities to the language syntax in .NET Introduces standard, easily-learned patterns for querying and updating data Extendable
  2. LINQ Queries: Overview 3 parts of a LINQ Query: //

    1. Data source. int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; // 2. Query creation. // numQuery is an IEnumerable<int> var numQuery = from num in numbers where (num % 2) == 0 select num; // 3. Query execution. foreach (int num in numQuery) { Console.Write("{0,1} ", num); }
  3. LINQ Queries: Query operation Data source Query from... where... select...

    Execution foreach (int item in ) Query Return each item item Do something with item … Get next item
  4. LINQ Queries: Data sources In memory o IEnumerable o IList

    o Array (or []) o ICollection Derived on-demand o IQueryable
  5. LINQ Queries: Comparison with SQL SELECT * FROM People AS

    p WHERE p.City = 'Johannesburg' ORDER BY p.Surname from p in People where p.City == "Johannesburg" orderby p.Surname select p; LINQ Query: SQL Query:
  6. LINQ Queries: Using Lambdas var joburgPeople = from p in

    People where p.City == "Johannesburg" orderby p.Surname select p; Using Lamda Expressions: var joburgPeople = People.Where(p => p.City == "Johannesburg") .OrderBy(p => p.Surname) .Select(p => p);
  7. LINQ to * Amazon Active Directory CSV Datasets Entities Excel

    Facebook Files Google Oracle RegEx SQL Twitter XML etc…
  8. LINQ to *: Case study For the stock code “MSFT”

    we need to search Facebook for the people who likes it as well as Twitter for the followers of Microsoft, then combine it with all our customers under the age of 55 that lives in any of our operational areas including the latest price from NASDAQ. This information need to be merged, ordered by surname and grouped by age. We need to export it to an Excel Spreadsheet.