我们知道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. Java集合----List集合

    List List 代表一个元素有序.且可重复的集合,集合中的每个元素都有其对应的顺序索引List 允许使用重复元素,可以通过索引来访问指定位置的集合元素.List 默认按元素的添加顺序设置元素的索引 ...

  2. 关于直播学习笔记-002-Red5 & Sewise Player & Wirecast

    一.工具软件 [1]. 视频采集端 Red5 Demo:http://192.168.31.107:5080/demos/simpleBroadcaster.html Telestream:Wirec ...

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

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

  4. UIGestureRecognizer学习笔记2

    The concrete subclasses of UIGestureRecognizer are the following: UITapGestureRecognizer UIPinchGest ...

  5. PyQt4将窗口放在屏幕中间

    以下脚本显示了将窗口放在屏幕中间位置的方法. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import QtGui c ...

  6. LeetCode——Valid Anagram

    Description: Given two strings s and t, write a function to determine if t is an anagram of s. For e ...

  7. sencha touch 入门系列 (七)sencha touch 类系统讲解(上)

    在mvc结构的基础上,sencha touch又使用了sencha公司为extjs4开发出来的类系统,在面向对象的编程语言中,类是对对象的定义,它描述了对象所包含的大量属性和方法. 跟面向对象语言类似 ...

  8. mysql表大小写问题

    查看大小写区分 mysql> show variables like "%case%"; linux在mysql安装完后默认:区分表名的大小写,不区分列名的大小写 改变表名的 ...

  9. java之面向对象三大特征(封装,继承,多态)

    一.封装 封装是指将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过该类提供的对外方法进行内部信息的操作和访问. 封装可以达到以下目的: 1)隐藏类的实现细节 2)让使用者只 ...

  10. 公民身份号码校验码算法(C#版)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...