我们知道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的更多相关文章

  1. Using LINQ to SharePoint

    LINQ and LINQ Providers   LINQ is a feature of the programming languages C# and Microsoft Visual Bas ...

  2. Linq to sharepoint

    一.Linq to SharePoint 首先Linq to SharePoint编程语言 C# 和 Microsoft Visual Basic .NET 的一个功能,编译器是 Visual Stu ...

  3. Linq to SharePoint与权限提升(转)

    转自http://www.cnblogs.com/kaneboy/archive/2012/01/25/2437086.html SharePoint 2010支持Linq to SharePoint ...

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

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  5. Linq之旅:Linq入门详解(Linq to Objects)【转】

    http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html Linq之旅:Linq入门详解(Linq to Objects) 示例代码下载:Linq之 ...

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

    http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html 示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细 ...

  7. Linq世界走一走(LINQ TO XML)

    前言:Linq to xml是一种使用XML的新方法.从本质上来说,它采用了多种当前使用的XML处理技术,如DOM和XPath,并直接在.NET Framework内将它们组合为一个单一的编程接口.L ...

  8. linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ

    在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...

  9. LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法

    本篇继续LINQ Operators的介绍,包括元素运算符/Element Operators.集合方法/Aggregation.量词/Quantifiers Methods.元素运算符从一个sequ ...

随机推荐

  1. linux连接sybase数据库-isql

    转自:http://blog.knowsky.com/196438.htm 想要linux连接sybase数据库用命令isql: isql [-U login id] [-P password] [- ...

  2. 新唐M0 M4系统初始化

    系统初始化包含了时钟(clock)初始化和多功能引脚(Multi Function Pin 简称MFP寄存器)配置.void SYS_Init(void) { /* 解锁保护寄存器 */ SYS_Un ...

  3. 关于js中定时器的返回值问题

    在js中,我们常常会用到定时器来处理各种各样的问题,当我们需要清除定时器的时候,我们常常会定义一个值来接受定时器的返回值,然后再把定义好的这个值写到清除定时器的括弧后面,如: var times = ...

  4. c#截取图片

    简单的保存数据流 <input name="upImg" style="width: 350px; height: 25px;" size="3 ...

  5. 窗口大小改变绑定resize事件

    当为窗口绑定resize事件时,大部分浏览器会在每改变一个像素就触发一次resize事件.这严重影响了网站的性能. 解决方案是:利用settimeout方法为事件发触发的方法设置延迟执行的时间. 实现 ...

  6. centos7关闭自动锁屏

    centos 7默认几分钟不动就锁屏,实在很讨厌,所以在设置中将其去掉 1.不同的版本应该设置的地方稍有变化,我不敢保证我的这个方法你一定可以用,所以用之前希望你了解你的系统的版本. 2.左上角点击应 ...

  7. 【BZOJ4524】[Cqoi2016]伪光滑数 堆(模拟搜索)

    [BZOJ4524][Cqoi2016]伪光滑数 Description 若一个大于1的整数M的质因数分解有k项,其最大的质因子为Ak,并且满足Ak^K<=N,Ak<128,我们就称整数M ...

  8. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  9. Flash 用FLASH遮罩效果做图片切换效果

    本教程是关于FLASH应用遮罩效果制作好看的图片切换效果.该教程选用FLASH遮罩中最简单的一种作为例子,当然你可以用自己的想象力来做出更多更好的图片动画.希望本教程能带你带来帮助. 让我们先看看效果 ...

  10. Unity3D 笔记二 3D模型基础

    一.3D模型基础 1.Hierarchy 显示的是界面上的游戏对象(GameObject),每个游戏对象都有.至少要有一个Camera,点击Camera就可以在Preview中看到摄像机的视角画面.每 ...