LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

19
LinqToSharePoint SandBoxed Solution Shakir Majeed Khan http://sharepointtechies.wordpress.com/
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

Transcript of LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Page 1: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

LinqToSharePointSandBoxed Solution

Shakir Majeed Khanhttp://sharepointtechies.wordpress.com/

Page 2: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

MySelf

User Group Leader of SharePoint Techies, Working independently on SharePoint technologies. Trainer for  Microsoft Office SharePoint Server 2007 and  Window SharePoint Services 3.0 at AUC Technologies. 

http://junooni.wordpress.com/ [email protected] www.facebook.com/shakir.majeed

Page 3: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Agendasession is divided into two

LinqtoSharePointSandBoxed Solution

Questions

Page 4: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

What’s wrong with this

SqlConnection c = new SqlConnection(…);c.Open();SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0");cmd.Parameters.AddWithValue("@p0", "London“);DataReader dr = c.Execute(cmd);while (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2);}dr.Close();

Queries in quotes

Loosely bound arguments

Loosely typed result sets

No compile time checks

Page 5: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

What’s wrong with this

Page 6: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

LINQ (pronounced Link):Language INtegrated Query

Page 7: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

What is LINQ?

Language Integrated QuerySimplified, object-oriented way to queryBridges OOP and relational dataCompile-time checked queriesProvides IntelliSense inside Visual StudioUnified syntax for querying any data source

Page 8: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Linq to SQL

public class Customer { … }

public class Northwind : DataContext{ public Table<Customer> Customers; …}

Northwind db = new Northwind(…);var contacts = from c in db.Customers where c.City == "London" select new { c.Name, c.Phone };

Classes describe data

Strongly typed connections

Integrated query syntax

Strongly typed results

Tables are like collections

Page 9: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

CAML(Collaborative Application MarkUp Language)SPWeb web;SPQueryquery = newSPQuery();query.Query= String.Format(“<Where> <And>  <Eq>        <FieldRef Name='LastName' />     <Value Type='Text'>Shakir</Value>   </Eq>     <Geq>        <FieldRef Name='Age' />    <Value Type='Number'>16</Value>  </Geq>  </And></Where>”)SPListItemCollectionl Coll= web.Lists[“Employee”].GetItems(query);

Queries in quotes

Loosely bound arguments

No compile time checks

Page 10: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Demo

Page 11: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

C# Enhancments for LINQ

IEnumerable<T>, IQueryable<T>Automatic PropertiesObject and Collection InitializersLambda ExpressionsQuery SyntaxAnonymous Types

Page 12: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

IEnumerable<T>, IQueryable<T>

Contained in System.Collections.GenericImplemented by the query providerSupports a simple iteration or a query against that typeThis is what allows LINQ to query

Page 13: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Automatic Properties

Allows short-hand property declarationsCompiler emits getters and settersUsed in LINQ to surface properties

public class Contact { public string FirstName { get; set; }  public string LastName  { get; set; }        }

Page 14: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Object and Collection Initializers//Traditional approach

Contact c= new Contact();c.FirstName = “shakir";c.LastName = “majeed ";

//Object InitializerContact c= new Contact{ FirstName=“shakir", LastName=“Majeed

" };

//Collection InitializerList<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" }, };

Page 15: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Lambda Expressions

Compact way to write anonymous functions

// passing parameter using an anonymous delegateIEnumerable<Contact> results = contacts.Where( delegate(Contact c) {return c.FirstName==“shakir”;} );

// equivalent code using a lambda expressionIEnumerable<Contact> results = contacts.Where( c => FirstName=="shakir" );

Page 16: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

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");

Page 17: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

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");

Page 18: LinqToSharePoint SandBoxed Solution Shakir Majeed Khan

Demo