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 ...
随机推荐
- IIS中查看W3P.exe进程对应的应用程序池的方法
对于IIS6可以运行 iisapp -a来显示应用程序池的性能列表. iisapp.vbs它是一个脚本程序,在安装2003时需要SP1才有 而IIS7可以直接用它的外壳命令 C:"Windo ...
- 301重定向.htaccess规则(含二级目录跳转二级域名)
301重定向是一种非常重要的"自动转向"技术.网址重定向最为可行的一种办法.当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码 ...
- node.js学习资料
Node.js 入门 <汇智网 Node.js 课程> <快速搭建 Node.js 开发环境以及加速 npm> http://fengmk2.com/blog/2014/03/ ...
- Flash 二进制传图片到后台Java服务器接收
需求:把客户端处理过的图片返还给服务器Flash端代码 01 package {02 import com.adobe.images.JPGEncoder; 03 import ...
- SpringMVC讲解
2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...
- tyvj[1089]smrtfun
描述 现有N个物品,第i个物品有两个属性A_i和B_i.在其中选取若干个物品,使得sum{A_i + B_i}最大,同时sum{A_i},sum{B_i}均非负(sum{}表示求和). 输入格式 ...
- PPT文档页数显示的增加和更新
在PPT的右下角增加页数的显示能够帮助演讲者把握进度,所以会经常遇到需要把页数显示在右下角的情况,这次在制作ppt的时候也遇到了.因此在这里总结一下设置方法. 一.在右下角显示当前页数和总页数 1)获 ...
- SQL/LINQ/Lamda
SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees .Sele ...
- 关于oracle with as用法
with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with tmp as (select * from tb_name), ...
- Screen 对象
Screen 对象 Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 注意: 没有应用于 screen 对象的公开标准,不过所有浏览器都支持该对象. Screen 对象属性 属性 ...