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"]));
}

复制代码

posted @ 2015-10-27 13:10  hornet_team  阅读(343)  评论(0编辑  收藏  举报