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 ...
随机推荐
- IBM云的商业动作之我见(1):IBM 收购 OpenStack 托管私有云公司 Blue Box [IBM Acquired Blue Box]
2015-06-10 IBM 刚刚(2015/06/03)宣布收购 Blue Box 公司.本文就聊聊这点事. 1. Blue Box 是做什么的?它是一家中小型托管私有云提供商. 1.1 公司的简单 ...
- MATLAB学习(一)——状态好状态坏,自作自受
状态不好,学学MATLAB做做准备吧. 一.基本情况 1.1 书写 一行写不下? %可以加上三个小黑点(续行符)并按下回车键,然后接下去再写.例如 s=-/+/-/+/-/+/-…- /+/-/+/- ...
- dp88dp6最靠谱的网络赚钱方法
(本文非原创,转载自http://mt.sohu.com/20160131/n436463696.shtml) 1.卖产品 最靠谱的当然是自己卖产品,可以先去淘宝.阿里巴巴.百度找到一款你认为有前景的 ...
- Codeforces水题集合[14/未完待续]
Codeforces Round #371 (Div. 2) A. Meeting of Old Friends |B. Filya and Homework A. Meeting of Old Fr ...
- Interceptor
拦截器是可以控制权限,当用户需要查看查看某些功能的时候,需要判断是不是登录了,如果没有登录的,就可拦截的过程.. 首先,我们都知道struts.xml 中有action 节点, 这个节点表示你想要访问 ...
- SQL变量、运算符、分支、循环语句
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...
- noip2013 积木大赛
题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...
- 使用gulp将移动端px转为rem
使用gulp的插件可以很方便的将xp转为rem,在布局的时候使用@1x .@2x布局,即10rem=device-width:@1x即设计图为320px,1rem对应的10px像素,相对的@2x即为布 ...
- 基于jquery的tips悬浮消息提示插件tipso
<a href="javascript:;" class="disabled" data-tipso="Tips" id=" ...
- uwp项目总结