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=, 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的更多相关文章
- Linq to sharepoint
一.Linq to SharePoint 首先Linq to SharePoint编程语言 C# 和 Microsoft Visual Basic .NET 的一个功能,编译器是 Visual Stu ...
- Linq to SharePoint与权限提升(转)
转自http://www.cnblogs.com/kaneboy/archive/2012/01/25/2437086.html SharePoint 2010支持Linq to SharePoint ...
- XML To Linq 读取Sharepoint列表中的附件列信息
通过页面查看,列表附件信息列的内容如下: var x = @"<div class='ExternalClass9936DCD1F074427B891D09CFCEFC2AB6'> ...
- SharePoint 服务器端对象模型 之 使用LINQ进行数据访问操作(Part 2)
(四)使用LINQ进行列表查询 在生成实体类之后,就可以利用LINQ的强大查询能力进行SharePoint列表数据的查询了.在传统SharePoint对象模型编程中,需要首先获取网站对象,再进行其他操 ...
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
(六)高效合理的使用LINQ 1.DataContext中的两个属性 为了能够使用DataContext进行数据提交,在DataContext进行数据查询和操作的过程中,内部会进行数据状态的保持和追踪 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 《SharePoint 2013 应用开发实战》目录
博客地址:http://blog.csdn.net/FoxDave 第 1 章 1 ◄SharePoint概述► 1 1.1 SharePoint的发展历程 1 1.1.1 Sha ...
- [.net 面向对象编程基础] (20) LINQ使用
[.net 面向对象编程基础] (20) LINQ使用 通过上节LINQ的基础知识的学习,我们可以开始使用LINQ来进行内存数据的查询了,我们上节说了LINQ的定义为:Language Integr ...
- SharePoint—用REST方式访问列表
REST的定义与作用 在SharePoint 2010中,基本上有如下几种数据访问方式: 服务器端对象模型 LINQ to SharePoint Web Service 客户端对象模型 ADO.NET ...
随机推荐
- Linux 下从头再走 GTK+-3.0 (五)
实践中表明,纯粹利用 gtk 函数来创建 UI 是很繁琐的事,需要编写很多代码.怎样才能快速统一的建立 UI 布局呢? 可喜的是 GTK 提供了一个 GtkBuilder 用于快速创建界面.它读取一个 ...
- 关于C语言中的位域
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态,用一位二进位即可.为了节省存储空间,并使处理简便,C语言提供了一种数据结构,称 ...
- 在VMware上安装CentOS-6.5 minimal - 安装VMware Tools
由于CentOS-6.5 minimal很多工具都默认没有安装,安装VMwareTools需要用到Perl,所以老伯建议先配置好网络再接着安装. 网络配置方法可以参考在VMware上安装CentOS- ...
- [转]Asp.net MVC 利用PartialView 构造自定义菜单
本文转自:http://www.cnblogs.com/huyq2002/archive/2012/01/06/2314838.html 在VS2010中利用Asp.net MVC自带的模板生成的菜单 ...
- 第5章 绘图基础_5.1-5.4 GDI绘图
5.1 GDI的原理和结构 (1)提供一种特殊机制彻底隔离应用程序与不同输出设备(eg.显示器或打印机),以便支持 与设备无关的图形. 光栅设备(如显示器.激光打印机):图像是由点构成的矩阵 图形输出 ...
- AC日记——单词倒排 1.7 28
28:单词倒排 总时间限制: 1000ms 内存限制: 65536kB 描述 编写程序,读入一行英文(只包含字母和空格,单词间以单个空格分隔),将所有单词的顺序倒排并输出,依然以单个空格分隔. 输 ...
- Unity Shaders and Effets Cookbook
Unity Shaders and Effects Cookbook 最近在读 <Unity Shaders and Effects Cookbook> 中文名称:<Unity 着色 ...
- SQL各种语句、函数
1.更改数据库的名称 2.表中有数据的情况下再添加列.删除列 3.在SQLServer 中各种查询语句的使用示例 4.模糊查询的语句 5.排序语句.排序并查前三名的语句 聚合函数:sum,avg,ma ...
- [No000053]我25岁了,是应该继续挣钱,还是选择自己的爱好?--正好庆祝自己25岁生日
你所问的问题正是问题所在.停止做出重大决策,专注于缩小你想到达的地位与你之间的差距. 成功的生活并非由简单而鲜明的决定组成,它们更像这幅图: 但悲伤的是,太多人的状态类似于这幅图: 我知道这听上去很显 ...
- dubbo2.5.3 与spring 3.1.x 冲突
在集成了dubbo2.5.3 的项目中初始化出现 MalformedParameterizedTypeException 检查发现这是因为dubbo2.5.3依赖的springframeworks是2 ...