1 / 22

LINQ - 1

LINQ - 1. Ravi Kumar C++/C# Team. What is LINQ!. Language-Integrated Query. Query facilities to the .NET Framework. Applies to all sources of IEnumerable <T> - based information, not just relational or XML data. Bad Old days!. Code to query using C# ADO.NET:

boone
Download Presentation

LINQ - 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LINQ - 1 Ravi Kumar C++/C# Team

  2. What is LINQ! • Language-Integrated Query. • Query facilities to the .NET Framework. • Applies to all sources of IEnumerable<T> - based information, not just relational or XML data.

  3. Bad Old days! • Code to query using C# ADO.NET: string query = "insert into EMPLOYEE_TABLE (EmployeeID, Name, Address) VALUES (101, 'John', '3960 CliffValley Way')"; OleDbCommandmyCommand = new OleDbCommand(); myCommand.CommandText = query; myCommand.ExecuteNonQuery(); • No compile-time syntax checking, static typing and IntelliSense

  4. Good days ahead… • Type-safe. • Compile-time checking of: • Query Syntax, • Variables, and • The results of a query.

  5. LINQ Architecture • CLR has no new IL instructions added to support LINQ. • LINQ support is entirely in assembly APIs and compiler implementation of the new language features.

  6. A LINQ Query…

  7. Partial Methods!! • Methods living in partial classes which are marked as partial. • Partial classes allow us to split the definition of a class across multiple files. • Flexibility.

  8. Partial Method Example     partial class PartialClass// (1)     {        static partial void OnSomethingHappened(inti);     }     // This part can be in a separate file.     partial class PartialClass // (2)     {         static void Main()         {             // Call is discarded if the implementation is  removed OnSomethingHappened(10);         }           // Comment out this method and the program         // will still compile.         // The implementation         static partial void OnSomethingHappened(inti)         { Console.WriteLine("Something happened: {0}", i);         }     } class partialClass     {         static void Main()         { OnSomethingHappened(10);         }         static void OnSomethingHappened(inti)         { Console.WriteLine("Something happened: {0}", i);         }     } class partialClass     {         static void Main()         {         }     }

  9. Local Variable Type Inference • Infer the data-type from the expression. • The varkeyword! • The var in C# is statically and strongly typed. Give me a local variable of the type of the expression on the right hand side.

  10. Var Examples!! // C# 2.0 declarations inti = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] { 1, 2, 3 }; Dictionary<int, string> orders = new Dictionary<int, string>(); // Equivalent C# Variable Type Inference declarations        vari = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] { 1, 2, 3 }; var orders = new Dictionary<int,string>();

  11. When to use var! • Can reduce readability. • Generics - long and complex type declarations. // Regular declaration Dictionary<Dictionary<int,string>,List<Dictionary<int,string>>> y =    new Dictionary<Dictionary<int,string>,List<Dictionary<int,string>>>(); // using var keyword (local variable type inference) var z = new Dictionary<Dictionary<int, string>, List<Dictionary<int, string>>>(); • Anonymous types.

  12. Extension Method • Extension of an existing type with new methods. • BCL types can be extended. string[] test = new string[] { "One", "Two", "Three" }; string s = test.Concatenate("|"); //Concatenate is User-defined Extended IEnumerable<string> with a method called Concatenate

  13. How-to create EM?? • Follow 4 rules: • Define in static class. • Declare as a public static. • first method argument - same type as the data type we want to extend. • C# 3.0 this modifier keyword on the first argument.

  14. Much needed example! namespace MyStuff {   public static class Extensions   { public static string Concatenate(thisIEnumerable<string> strings,                                      string separator)     { StringBuildersb = new StringBuilder(); foreach (string s in strings)         { sb.Append(s); sb.Append(separator);         } sb.Remove(sb.Length - 1, 1);         return sb.ToString();     }   } } using MyStuff; … string[] test = new string[] { "One", "Two", "Three" }; string s = test.Concatenate("|");

  15. Inside story! • NO new IL or other CLR support. • Compiler has an added function lookup rule! • 2 steps: • Look for pre-defined methods. • Look for static methods with appropriate 1st parameter. • obj.Foo(x)                     xxx.Foo(obj, x) Any static class in scope

  16. Automatic Properties • Properties - concise and compact syntax. C# 2.0 public class Point { private int x, y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; }         set { y = value; } } } C# 3.0 public class Point { public int X { get; set; } public int Y { get; set; } }

  17. Lambda Expression!! • “=>” : “goes to”, “becomes”, “for which”, “maps to” • Inline delegate methods. • X => X + 1 • LHS: delegate parameters. • RHS: delegate body.

  18. Need an example?? C# 2.0 C# 3.0 static void Main(string[] args) { List<string> numbers = new List<string>(); numbers.Add("One"); numbers.Add("Three"); string four = numbers.Find( delegate(string number) { return number.Equals("Four"); } ); } static void Main(string[] args) { var numbers = new List<string> {"One","Three”}; string four = numbers.Find(n => n.Equals("Four")); Console.WriteLine(four); } Del Param LHS Del Body RHS

  19. Object Initializers! • Dynamically create and initialize public properties of an object. C# 2.0 C# 3.0 Point p = new Point(); p.X = 0; p.Y = 1; Point p = new Point { X = 0, Y = 1 };

  20. Rest in the next session • Anonymous types. • Query Expressions. • Expression Trees.

  21. References • Must watch video: Anders Hejlsberg on LINQ and Functional Programming • Find more links here • C# PM's Karen Liu and DJ Park on the C# IDE, LAF, and support for LINQ.

  22. Questions Plz!!

More Related