We know that LINQ to SharePoint actually eventually turns into calm to access SharePoint, so how do we know what the calm statement we write the query statement will ultimately look like. We can use the following methods to achieve our goal.
1. Start by creating a new class named Camldebug in our SharePoint project:
The CALMDebug.cs code is as follows:
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. Then use this class in our LINQ to SharePoint code
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. Set breakpoints in the code snippet and go to debug (of course, you can also export the queries saved calm string to anywhere you want)
4. Here, some people do not use the above class, and directly using the following code to directly output the generated calm to the specified TXT file for viewing.
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 ();
The result of the above code output is as follows:
The next task is to make a reference in your SPQuery (here is a reference sample, for reference only)
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--How to get calm generated by LINQ Query