linq 1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
LINQ - 1 PowerPoint Presentation
Download Presentation
LINQ - 1

Loading in 2 Seconds...

play fullscreen
1 / 22

LINQ - 1 - PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on

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:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'LINQ - 1' - boone


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
linq 1

LINQ - 1

Ravi Kumar

C++/C# Team

what is linq
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
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
Good days ahead…
  • Type-safe.
  • Compile-time checking of:
    • Query Syntax,
    • Variables, and
    • The results of a query.
linq architecture
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
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 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
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
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
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 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
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
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
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
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
Lambda Expression!!
  • “=>” : “goes to”, “becomes”, “for which”, “maps to”
  • Inline delegate methods.
  • X => X + 1
    • LHS: delegate parameters.
    • RHS: delegate body.
need an example
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
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
Rest in the next session
  • Anonymous types.
  • Query Expressions.
  • Expression Trees.
references
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.