Linq to Sharepoint--如何获取Linq Query 生成的CALM
我们知道Linq to sharepoint 实际最终还是转化成了CALM来对Sharepoint进行访问,那么我们怎样才能知道我们编写的Query语句最终转化成的CALM语句是什么样子呢。 我们可以使用如下方法来达到我们的目的。
1.首先在我们的Sharepoint项目中新建一个名为CAMLDebug的类,如图:
CALMDebug.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint.Linq;
namespace NorthwindLinqToSP
{
public class CAMLDebug : IDisposable
{
private DataContext _context;
public StringWriter Writer
{
get;
private set;
}
public CAMLDebug(DataContext context)
{
_context = context;
Writer = new StringWriter();
_context.Log = Writer;
}
public override string ToString()
{
Writer.Flush();
return Writer.GetStringBuilder().ToString();
}
public void Dispose()
{
_context.Log = null;
Writer.Dispose();
}
}
}
2.然后在我们的Linq to sharepoint 代码中使用此类
var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);
MyCustomers = dc.GetList<ACustomerItem>("ACustomer");
MyOrders = dc.GetList<AOrdersItem>("AOrders");
using (CAMLDebug debug = new CAMLDebug(dc))
{
string queries = debug.ToString();
var query = from c in MyCustomers
where (from o in MyOrders
select o.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
select c;
this.lblMsg2.Text = "Items :" + query.Count().ToString();
this.gvDetails.DataSource = query;
this.gvDetails.DataBind();
}
3.在代码段中设置断点,进入调试(当然,你也可以把queries保存的CALM字串输出到你想要的任何地方)
4.此处,也有人不用上面的类,而直接使用如下代码把生成的CALM直接输出到指定的txt文件中进行查看。
var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);
MyCustomers = dc.GetList<ACustomerItem>("ACustomer");
MyOrders = dc.GetList<AOrdersItem>("AOrders");
TextWriter textWriter = new StreamWriter(@"c:\caml.txt", false);
dc.Log = textWriter;
var query = from c in MyCustomers
where !(from o in MyOrders
select o.BCSFindCustomerID).Contains(c.BCSFindCustomerID)
select new
{
CopanyName = c.BCSFindCompanyName,
ContanctName = c.BCSFindContactName,
Address = new
{
Country = c.BCSFindCountry,
City = c.BCSFindCity,
PostalCode = c.BCSFindPostalCode
}
};
this.lblMsg2.Text = "Items :" + query.Count().ToString();
this.gvDetails.DataSource = query;
this.gvDetails.DataBind();
上述代码输出的结果如下图:
接下来的任务就是在你的SPQuery 中进行引用了(以下是一个引用样例,仅作参考)
SPQuery query = new SPQuery();
query.Query = @"<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentTypeId' />
<Value Type='ContentTypeId'>0x0100</Value>
</BeginsWith>
<BeginsWith>
<FieldRef Name='ProductContentTypeId' />
<Value Type='Lookup'>0x0100</Value>
</BeginsWith>
</And>
</Where>
<OrderBy Override='TRUE' />";
query.ViewFields = @"<FieldRef Name='Title' />
<FieldRef Name='ProductProductName' />";
query.ProjectedFields = @"<Field Name='ProductProductName' Type='Lookup'
List='AProduct' ShowField='ProductName' />
<Field Name='ProductContentTypeId' Type='Lookup'
List='AProduct' ShowField='ContentTypeId' />";
query.Joins = @"<Join Type='INNER' ListAlias='AProduct'>
<Eq>
<FieldRef Name='Product' RefType='ID' />
<FieldRef List='Product' Name='ID' />
</Eq>
</Join>";
query.RowLimit = 2657495668;
var list = web.Lists["AOrders"];
var items = list.GetItems(query);
foreach (SPListItem item in items)
{
this.ListBoxOutPut.Items.Add(item["Title"]+ item["ProductProductName"]));
}

Linq to Sharepoint--如何获取Linq Query 生成的CALM的更多相关文章
- Using LINQ to SharePoint
LINQ and LINQ Providers LINQ is a feature of the programming languages C# and Microsoft Visual Bas ...
- 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 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- Linq之旅:Linq入门详解(Linq to Objects)【转】
http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html Linq之旅:Linq入门详解(Linq to Objects) 示例代码下载:Linq之 ...
- Linq之旅:Linq入门详解(Linq to Objects)(转)
http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html 示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细 ...
- Linq世界走一走(LINQ TO XML)
前言:Linq to xml是一种使用XML的新方法.从本质上来说,它采用了多种当前使用的XML处理技术,如DOM和XPath,并直接在.NET Framework内将它们组合为一个单一的编程接口.L ...
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法
本篇继续LINQ Operators的介绍,包括元素运算符/Element Operators.集合方法/Aggregation.量词/Quantifiers Methods.元素运算符从一个sequ ...
随机推荐
- HTML&CSS精选笔记_表格与表单
表格与表单 表格标记 创建表格 要想创建表格,就需要使用表格相关的标记 <table> <tr> <td>单元格内的文字</td> ...
- Java 基本语法----数组
数组 数组概述 数组是多个相同类型数据的组合,实现对这些数据的统一管理. 数组属引用类型,数组型数据是对象(Object),数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任何数据类型,包 ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- eclipse不能自动编译XX.java为XX.classs
问题描述:eclipse不能自动编译XX.java为XX.classs 原因:今天下午写代码,因为需要引入jstl包,引入后发现原来项目中已经引入了,然后我又把包删除了,忘记删除java build ...
- docker 配置文件:/etc/docker/daemon.json
/etc/docker/daemon.json 是 docker 的配置文件,默认是没有的,需要我们手动创建,可配置项如下: [root@localhost ~]$ vim /etc/docker/d ...
- 关于MCU的烧录,下载与其他接口的比较(一)
今天呢,犯了一个很严重的错误,我不知道这会产生什么样的影响,但我知道,如果我以后再没有具体的了解,仔细认真地观察,认证,只会滑到无底的深渊.做技术来不得半点虚假,切记一知半解,凡事都要弄得清楚明白,认 ...
- vertical-align负值和margin-bottom负值的区别
先看一下vertical-align在W3C当中的值有哪一些: 可是它有数值这一说确实很少提起,我们来看这么一段代码: <!DOCTYPE html> <html lang=&quo ...
- 【PHP+Redis】 php-redis 操作类 封装
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- Docker源码分析(五):Docker Server的创建
1.Docker Server简介 Docker架构中,Docker Server是Docker Daemon的重要组成部分.Docker Server最主要的功能是:接受用户通过Docker Cli ...
- \r\n和\n的区别
写Java代码的时候习惯用\r\n换行,这样可移植性比较好但是,在UVa - 160中就出现了错误,来看看是为什么吧. http://bbs.csdn.net/topics/220033879