Using LINQ to SharePoint

Source: Internet
Author: User

LINQ and LINQ Providers

LINQ is a feature of the programming languages C # and Microsoft Visual Basic. NET. Compilers is included with Visual Studio.

LINQ adds a sql-like syntax and vocabulary to each of the languages, which can is used to query data sources. But unlike other languages and query syntaxes which vary from one type of data source to another, LINQ can is used to Quer Y, in principle, any data source whatsoever. For this reason, developers may find, it's the only query syntax that they ever need to know.

All this is necessary to make a data source accessible with LINQ are that someone create a LINQ provider for the data so Urce. A LINQ Provider is an implementation of the system.linq.iqueryable<t> and  System.linq.iqueryprovider interfaces that is included with the Microsoft. NET Framework (System.Core.dll). The implementing classes must is public in a managed code assembly. The primary job of the class, implements iqueryprovider is to translate, LINQ queries into the language of th e data source, such as SQL or XQuery, then call the data source ' s application to execute the query.

The provider must also expose a gateway class whose instances can communicate with the data source and Output ienu Merable<t>objects. For example, the Gateway class for linq to sql is datacontext and the Gateway class for linq to X Ml is xdocument. The Gateway class must implement system.linq.iqueryable<t> or has a child property which does so or has a Method that returns a type that implements system.linq.iqueryable<t>. For Example, datacontext has a gettable ()  method that returns a table<tentity>  type that implements system.linq.iqueryable<t>. The latter interface, in turn, have a property of Type iqueryprovider. (The Gateway class can also directly implement iqueryprovider.) It is objects of Type table<tentity> that was queried.

In many cases, a LINQ Provider cannot is used by a. NET Solution Developer unless the developer creates a set of entity CL Asses to represent the subordinate entities in the data source, such as the particular tables of a particular SQL database . Frequently, a lot of such classes must is created (e.g., a database with three dozen tables), so the developer of a LINQ p Rovider would typically include a code generation tool to automate the process of creating these entity classes.

Learning about LINQ

Before working with the SharePoint to LINQ Provider, developers should learn about LINQ in general and how it's used with The LINQ to SQL provider and the special LINQ to Objects provider, that is included with the. NET Framework.

Microsoft Developer Network (MSDN) has a great deal of information, including some videos, about LINQ and the providers bu Ilt into the. NET Framework. There is also many books on the subject.

The LINQ to SharePoint Provider

The LINQ to SharePoint Provider are defined in the Microsoft.SharePoint.Linq namespace. It translates LINQ queries into collaborative application Markup Language (CAML) queries. It is no longer necessary for developers to know how to write CAML queries. LINQ queries can is used in server code. To query from a client application, use SharePoint's support for ADO Data Services.

The Gateway Class for the LINQ-to-SharePoint provider is Microsoft.SharePoint.Linq.DataContext which represents the data O f a SharePoint Foundation Web site. It is parallel in use and function to the System.Data.Linq.DataContext class in the Linq to SQL provider. Just as the latter class has a GetTable () method, returns a Table<tentity> object that implements System.Linq.IQ Ueryable<t>, so too, TheMicrosoft.SharePoint.Linq.DataContext class have a getlist<t> method that returns an En Titylist<tentity> class that implementssystem.linq.iqueryable<t>. It is objects of type entitylist<tentity> that is queried.

The following is a example of the use of the LINQ to query SharePoint Foundation.

C#
//Get DataContext from page contextDataContext data =NewDataContext (SPCONTEXT.CURRENT.WEB.URL);//Get the SharePoint listentitylist<customer> Customers = data. Getlist<customer> ("Customers");//Query for customers from LondonvarLondoncustomers = fromCustomerinchCustomerswhereCustomer. City = ="London"                      Selectcustomer;foreach(varLondoncustinchlondoncustomers) {Console.WriteLine ("id = {0}, city = {1}", Londoncust.customerid, londoncust.city);}

For more information on querying with LINQ to SharePoint, see how To:query Using LINQ to SharePoint.

The following is an example of using LINQ to add an item to a SharePoint Foundation list.

C#
//Get DataContext from page contextDataContext data =NewDataContext (SPCONTEXT.CURRENT.WEB.URL);//Get the SharePoint listentitylist<customer> Customers = data. Getlist<customer> ("Customers");//Create the item to be addedCustomer Newcustomer =NewCustomer () {customerid= $, city="Madrid"};//Mark the item to being added on the next call of the SubmitCustomers.insertonsubmit (newcustomer);//Submit All ChangesData. SubmitChanges ();

For more information about adding, editing, and deleting list items with LINQ to SharePoint, see what to:write to Lists Us ing LINQ to SharePoint.

https://msdn.microsoft.com/en-us/library/office/ee535491 (v=office.14). aspx

Using LINQ to SharePoint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.