LINQ is a new data access paradigm which allows users to express SQL like syntax against a variety of data sources. LINQ can improve performance by allowing the back end data source to decide the best way to solve the query. SharePoint now fully supports LINQ for querying lists so that you can query information from the platform in a more condensed, easier to understand format.
LINQ to SharePoint
- No CAML Required
- Entity classes form Business Layer
- Strongly typed queries and compile time check
- Intelligence helps query construction
- Microsoft.SharePoint.Linq.dll
Using LINQ to SharePoint
- Create Entity Classes
- Create DataContext
- Write Queries
In order to use LINQ to SharePoint, you will need to have entity classes created. Rather than having to create them manually which would take a long time, Microsoft provided a small utility that creates all those objects for you called SPMetal (
click here for more information). The SPMetal utility works similarly to the SQLMetal utility (extracts SQL metadata from a database and generates entity classes). SPMetal will generate those entity classes which are used in LINQ to execute SharePoint queries. When those queries are executed, the LINQ statements are in turn translated to CAML.
The following is an example of the use of LINQ to query SharePoint :
// Get DataContext from page context
DataContext data =
new DataContext(SPContext.GetContext(
this.Context).Web.Url);
// Get the SharePoint list
EntityList
Customers = data.GetList("Customers");
// Query for customers from London
var londonCustomers = from customer in Customers
where customer.City == "London"
select customer;
foreach (var londonCust in londonCustomers)
{
Console.Writeline("id = {0}, City = {1}", londonCust.CustomerId, londonCust.City);
}
No comments:
Post a Comment