220 likes | 344 Views
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:
E N D
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: 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
Good days ahead… • Type-safe. • Compile-time checking of: • Query Syntax, • Variables, and • The results of a query.
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.
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.
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() { } }
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.
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>();
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.
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
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.
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("|");
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
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; } }
Lambda Expression!! • “=>” : “goes to”, “becomes”, “for which”, “maps to” • Inline delegate methods. • X => X + 1 • LHS: delegate parameters. • RHS: delegate body.
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
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 };
Rest in the next session • Anonymous types. • Query Expressions. • Expression Trees.
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.