Using LINQ to SharePoint

LINQ and LINQ Providers

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

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.

C#

// 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.

C#

// 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

时间: 2024-10-13 15:40:41

Using LINQ to SharePoint的相关文章

【Linq to SharePoint】对列表查询的分页技术制作新联列表分页

[Linq to SharePoint]对列表查询的分页技术 1. 下面是用AspNetPage来分页的,主要在网站在有一个列表名称为新闻列表,BindGridView是一个分页的函数. 下面的主要是对一个列表的查询进行的分页. 后台代码 EntityList<新闻列表项目> newsList; public const int PageSize = 10; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsP

Linq to sharepoint

一.Linq to SharePoint 首先Linq to SharePoint编程语言 C# 和 Microsoft Visual Basic .NET 的一个功能,编译器是 Visual Studio 附带的. Linq to SharePoint是LINQ家族中的一员,如果使用过例如Linq to SQL的话,就会发现Linq to SharePoint与其有很多相同之处. 如Linq to SQL中有DataContext类,用来打开数据库连接并进行操作.Linq to SharePo

在SharePoint中使用LINQ

在SharePoint中使用LINQ LINQ是一种有效且高效的查询数据的方法.使用SPMetal你可以为LINQ准备SharePoint列表.下面讲解如何准备你的列表,并创建使用LINQ的应用程序. 1. 打开命令行(管理员身份运行). 2. 导航到c:\Program Files\Common Files\Microsoft Shared\web server extensions\14\bin,并输入下面命令(确保用你自己的服务器名代替) spmetal.exe /web:http://<

SharePoint 服务器端对象模型 之 使用LINQ进行数据访问操作(Part 2)

(四)使用LINQ进行列表查询 在生成实体类之后,就可以利用LINQ的强大查询能力进行SharePoint列表数据的查询了.在传统SharePoint对象模型编程中,需要首先获取网站对象,再进行其他操作:使用LINQ也是类似,对数据的访问和操作都要先获取DataContext网站,之后再通过这个DataContext进行数据访问. 1.获取网站和列表 DataContext的创建同样也是要使用绝对路径进行构造,并使用GetList方法获取相应的列表.例如下面的代码获取Chapters列表中所有的

SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 3)

(五)使用LINQ进行数据更新 LINQ to SharePoint的优势不仅在于可以方便快捷地进行SharePoint列表数据查询,SPMetal所生成的数据实体类实际上是一个支持双向同步的模型,也就是意味着通过DataContext我们还可以将数据的变动提交到SharePoint列表中. 通过DataContext进行数据提交主要分成三个阶段:(1)确保DataContext支持数据提交,即其中的ObjectTrackingEnabled属性为true(默认值):(2)修改数据实体的内容:(

[.net 面向对象编程基础] (20) LINQ使用

[.net 面向对象编程基础] (20)  LINQ使用 通过上节LINQ的基础知识的学习,我们可以开始使用LINQ来进行内存数据的查询了,我们上节说了LINQ的定义为:Language Integrated Query(语言集成查询)的简称,它是集成在.NET编程语言中的一种特性. 1.LINQ的构架 从这幅图中,我们可以知道LINQ包括五个部分:LINQ to Objects.LINQ to XML.LINQ to SQL.LINQ to DataSet.LINQ to Entities.

SharePoint快速入门(2)之WebPart

本节学习目标:用Linq读取Sharepoint List数据,并通过SPGridView展示数据 生成 Linq to sharepoint代理类  通过命令行定位到“c:\program files\common files\microsoft shared\web server extensions\14\bin” 输入以下代码生成代理类 spmetal.exe /web:http://intranet.contoso.com /namespace:Jonny.VisualWebPart1

Linq之旅:Linq入门详解(Linq to Objects)

示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集成查询).通过LINQ,我们可以使用相同API操作不同的数据源.接下来就让我们看看LINQ是什么以及如何使用? 再此之前,需要先了解的相关技术 1. 隐式类型.匿名类型.对象初始化器 1) 隐式类型,使用var关键字创建,C#编译器会根据用于初始化局部变量的初始值推断出变量的数据类型.(不过我个人认

SharePoint的实体生成

生成Linq实体 使用SPMetal工具生成Linq to SharePoint实体 工具安装目录: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\SPMETAL.exe 命令参数: cmd: cdC:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN 生成 C# 代码: //10.250.201