LINQ to Sharepoint – MSDN

LINQ to SharePoint operates against strongly-typed entities that represent the items in your lists, and SharePoint 2010 includes a command-line tool named SPMetal that you can use to generate these entities. Internally, the LINQ to SharePoint provider generates the underlying CAML queries that perform the data operations.

3 steps: (1) Genrate the entity class (2) Develop the solution (3) Run the solution


Most likely you will use SPMetal to generate entity classes. It generates: (1) a data context class derived from DataContext; (2) Classes that represent content type and (3) Classes and properties that represent relationships between lists

SPMetal is here:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

E.g., SPMetal /web:http://bruce-pc/ /code:bruce-pc.cs

When you use the SPMetal command-line tool to generate entity classes, it automatically detects relationships between lists based on lookup columns, and it adds properties to the entity classes to enable you to navigate these relationships.

LINQ to Sharepoint


You should always dispoae the DatContext
after use. Using “using” or “dispose” or “Close”.

LINQ to SharePoint uses deferred loading—commonly known as lazy loading—of result sets to improve query efficiency. If you create a query that returns a collection of entities, the query won’t actually execute until you commence an action that uses the result set—such as iterating over the results or converting the result set to an array. LINQ query is only converted to CAML and executed when the foreach statement starts enumerating the result set.

LINQ to SharePoint uses the SPContext.Current
property to load the data context

In cases where LINQ to SharePoint cannot perform an operation using a single CAML query, the runtime will throw a NotSupportedException. (can be resloved by using View Projection)

When you create a LINQ expression, you typically use operators within a where clause to constrain your result set. However, the LINQ to SharePoint provider is unable to translate every LINQ operator into CAML. For example, the Equals operator and the HasValue operator
have no CAML equivalent. The LINQ to SharePoint provider will translate as many where clause operators as possible into CAML, and then it will use LINQ to Objects to fulfill the remaining criteria.

Don’t use unsupported LINQ synatx such as “EQUALS”

View Projection

you can substantially improve query efficiency by using view projections. A view projection queries a specific set of fields from one or more entities. When you want to retrieve a read-only view of a set of data, using a view projection restricts the number of fields returned by the query and ensures that joins are added to the CAML query instead of performed as a post-processing step.

eg. :

Permitted type for View Projection : Text, Number, DateTime, Count, and Content Type ID.


Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: