Using LINQ to SharePoint
Posted 致林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Using LINQ to SharePoint相关的知识,希望对你有一定的参考价值。
LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources. But unlike other languages and query syntaxes which vary from one type of data source to another, LINQ can be used to query, in principle, any data source whatsoever. For this reason, developers may find that it is the only query syntax that they ever need to know.
All that is necessary to make a data source accessible with LINQ is that someone create a LINQ provider for the data source. A LINQ provider is an implementation of the System.Linq.IQueryable<T> and System.Linq.IQueryProvider interfaces that are included with Microsoft .NET Framework (System.Core.dll). The implementing classes must be public in a managed code assembly. The primary job of the class that implements IQueryProvider is to translate LINQ queries into the language of the 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 IEnumerable<T>objects. For example, the gateway class for LINQ to SQL is DataContext and the gateway class for LINQ to XML is XDocument. The gateway class must implement System.Linq.IQueryable<T> or have a child property that does so or have 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, has a property of type IQueryProvider. (The gateway class can also directly implement IQueryProvider.) It is objects of type Table<TEntity> that are queried.
In many cases, a LINQ Provider cannot be used by a .NET solution developer unless the developer creates a set of entity classes 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 be created (e.g., a database with three dozen tables), so the developer of a LINQ provider will 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 is used with the LINQ to SQL provider and the special LINQ to Objects provider that are included with .NET Framework.
Microsoft Developer Network (MSDN) has a great deal of information, including some videos, about LINQ and the providers built into .NET Framework. There are also many books on the subject.
The LINQ to SharePoint Provider
The LINQ to SharePoint Provider is 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 be used in server code. To query from a client application, use SharePoint’s support for ADO.NET Data Services.
The gateway class for the LINQ to SharePoint provider is Microsoft.SharePoint.Linq.DataContext which represents the data of 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 that returns a Table<TEntity> object that implements System.Linq.IQueryable<T>, so too, theMicrosoft.SharePoint.Linq.DataContext class has a GetList<T> method that returns an EntityList<TEntity> class that implementsSystem.Linq.IQueryable<T>. It is objects of type EntityList<TEntity> that are queried.
The following is an example of the use of LINQ to query SharePoint Foundation.
// Get DataContext from page context DataContext data = new DataContext(SPContext.Current.Web.Url); // Get the SharePoint list EntityList<Customer> Customers = data.GetList<Customer>("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); }
For more information about 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.
// Get DataContext from page context DataContext data = new DataContext(SPContext.Current.Web.Url); // Get the SharePoint list EntityList<Customer> Customers = data.GetList<Customer>("Customers"); // Create the item to be added Customer newCustomer = new Customer() { CustomerId=36, City=”Madrid” }; // Mark the item to be added on the next call of Submit Customers.InsertOnSubmit(newCustomer); // Submit all changes data.SubmitChanges();
For more information about adding, editing, and deleting list items with LINQ to SharePoint, see How to: Write to Lists Using LINQ to SharePoint.
https://msdn.microsoft.com/en-us/library/office/ee535491(v=office.14).aspx
以上是关于Using LINQ to SharePoint的主要内容,如果未能解决你的问题,请参考以下文章
Three Steps to Migrate Group Policy Between Active Directory Domains or Forests Using PowerShell(示例代
[Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework(代
Spark启动:WARN util.Utils: Your hostname, ... resolves to a loopback address: ...; using ... instead(代
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE(示例代
解决Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] to(代