我们知道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. Loadrunner windows计数器

    object (对象) Counters (计数器名称) Description (描述) 参考值 Memory Available Mbytes 可用物理内存数.如果该值很小(4MB或更小),则说明 ...

  2. Python 爬虫知识点

    一.基础知识 1.HTML分析 2.urllib爬取 导入urilib包(Python3.5.2) 3.urllib保存网页 import urllib.requesturl = "http ...

  3. Apache Prefork、Worker和Event三種MPM分析

    三種MPM介紹 Apache 2.X  支持插入式並行處理模塊,稱爲多路處理模塊(MPM).在編譯apache時必須選擇也只能選擇一個MPM,對類UNIX系統,有幾個不同的MPM可供選擇,它們會影響到 ...

  4. 最近maven开发中遇到的一些bug。

    1.WebxContextLoaderListener  等tomcat启动报错.大部分原因都是jar包问题. 检查方式,在tomcat的webapps/WEB-INF/lib下有没有想对应的jar包 ...

  5. mac 获取idea&&datagrip激活码

    mac 版本的修改如下: 1). Command+Shift+G 2). /private/etc/ 3). 找到hosts文件,用文集编辑器打开 4). 输入0.0.0.0 account.jetb ...

  6. mysql增删改查基本语句

    mysql的增删改查属于基本操作,又被简称CRUD,其中删用的较少,毕竟这个功能给用户是是非常危险的,就是客户删除的数据也没有真正的删除,其中查询是十分常用的. 1 mysql数据库增加:create ...

  7. c++11——列表初始化

    1. 使用列表初始化 在c++98/03中,对象的初始化方法有很多种,例如 int ar[3] = {1,2,3}; int arr[] = {1,2,3}; //普通数组 struct A{ int ...

  8. 用Iterator实现遍历集合

    使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码. 示例: C ...

  9. Android中集成QQ登陆和QQ好友分享及QQ空间分享

    extends : http://blog.csdn.net/arjinmc/article/details/38439957 相关官方文档及下载地址: 如果只用分享和登陆,用lite包就可以,体积小 ...

  10. jquery插件方式实现table查询功能

    1.写插件部分,如下: ;(function($){ $.fn.plugin = function(options){ var defaults = { //各种属性,各种参数 } var optio ...