快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

和记h188怡情:【Sharepoint教程Linq to Sharepoint】3、获取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和记h188怡情 context)

{_context = context;

Writer = new StringWriter();_context.Log = Writer;

}

public override string ToString()和记h188怡情{

Writer.Flush();return Writer.GetStringBuilder().ToString();

}

public void Dispose(){

_context.Log = 和记h188怡情null;Writer.Dispose();

}}

}

2.然后在我们的Linq to sharepoint 代码中应用此类

var dc = new NorthWindEntityDataContext(SPContext.Current.Web.Url);

MyCustomers = dc.GetList("ACustomer");

MyOrders = dc.GetList("AOrders");

using (CAMLDebug debug = new CAMLDebug(dc)) {

string queries = debug.ToString();var query = from c in MyCustomers

where (from o in MyOrdersselect 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("ACustomer");MyOrders = dc.GetList("AOrders");

TextWriter textWriter = new StreamWriter(@"c:caml.txt", false);

dc.Log = textWriter;

var query = from c in MyCustomerswhere !(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 :" + qu和记h188怡情ery.Count().ToString();this.gvDetails.DataSource = query;

this.gvDetails.DataBind();

上述代码输出的结果如下图:

接下来的义务便是在你的SPQuery中进行引用了(以下是一个引用样例,仅作参考)

SPQuery query = new SPQuery();

query.Query = @"

'ContentTypeId' />

'ContentTypeId'>0x0100

'ProductContentTypeId' />

'Lookup'>0x0100

'TRUE' />";

query.ViewFields = @"'Title' />'ProductProductName' />";

query.ProjectedFields = @"'ProductProductName' Type='Lookup'List='AProduct' ShowField='ProductName' />

'ProductContentTypeId' Type='Lookup'List='AProduct' ShowField='ContentTypeId' />";

query.Joins = @"'INNER' ListAlias='AProduct'>

'Product' RefType='ID' />'Product' Name='ID' />

";

query.RowLimit = 2657495668;

var list = web.Lists["AOr和记h188怡情ders"]; var items = list.GetItems(query);

foreach (SPListItem item in items) {

this.ListBoxOutPut.Items.Add(item["Title"]+ item["ProductProductName"])); }

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: