3. Agenda
session is divided into two
LinqtoSharePoint
SandBoxed Solution
Questions
4. What’s wrong with this
Queries in
SqlConnection c = new SqlConnection(…); quotes
c.Open();
SqlCommand cmd = new SqlCommand(
@"SELECT c.Name, c.Phone Loosely bound
FROM Customers c arguments
WHERE c.City = @p0");
cmd.Parameters.AddWithValue("@p0", "London“);
DataReader dr = c.Execute(cmd); Loosely typed
while (dr.Read()) { result sets
string name = dr.GetString(0);
string phone = dr.GetString(1);
DateTime date = dr.GetDateTime(2); No compile time
} checks
dr.Close();
7. Language Integrated Query
Simplified, object-oriented way to query
Bridges OOP and relational data
Compile-time checked queries
Provides IntelliSense inside Visual Studio
Unified syntax for querying any data source
8. Linq to SQL
Classes
public class Customer { … } describe data
public class Northwind : DataContext
Tables are like
{ collections
public Table<Customer> Customers;
…
} Strongly typed
connections
Northwind db = new Northwind(…);
var contacts = Integrated
from c in db.Customers query syntax
where c.City == "London"
select new { c.Name, c.Phone };
Strongly typed
results
11. C# Enhancments for LINQ
IEnumerable<T>, IQueryable<T>
Automatic Properties
Object and Collection Initializers
Lambda Expressions
Query Syntax
Anonymous Types
12. IEnumerable<T>, IQueryable<T>
Contained in System.Collections.Generic
Implemented by the query provider
Supports a simple iteration or a query against
that type
This is what allows LINQ to query
13. Automatic Properties
Allows short-hand property declarations
Compiler emits getters and setters
Used in LINQ to surface properties
public class Contact {
public string FirstName { get; set; }
public string LastName { get; set; }
}
14. Object and Collection Initializers
//Traditional approach
Contact c= new Contact();
c.FirstName = “shakir";
c.LastName = “majeed ";
//Object Initializer
Contact c= new Contact{ FirstName=“shakir", LastName=“Majeed "
};
//Collection Initializer
List<Contact> contacts = new List<Contact>{
Contact c= new Contact{ FirstName=“shakir", LastName=“majeed"},
Contact c= new Contact{ FirstName=“aamir", LastName=“majeed" },
Contact c= new Contact{ FirstName=“zakir", LastName=“majeed" },
};
15. Lambda Expressions
Compact way to write anonymous functions
// passing parameter using an anonymous delegate
IEnumerable<Contact> results =
contacts.Where( delegate(Contact c) {return c.FirstName==“shakir”;} );
// equivalent code using a lambda expression
IEnumerable<Contact> results =
contacts.Where( c => FirstName=="shakir" );
16. Query Syntax
Offers a readable alternative to Method syntax
IEnumerable<Contact> results =
contacts.Where( c=> FirstName=="shakir");
IEnumerable<Contact> results =
from c in contacts
where c.FirstName.Equals("shakir");
17. Anonymous Types
Allows developers to use variables without
declaring the type.
Still strongly typed, but inferred
var results =
from c in contacts
where c.FirstName.Equals("Mike");
This slide shows the language enhancements for C# that directly support LINQ. These enhancements make it possible to write compact LINQ queries in a single statement.