SharePoint Study Notes-LINQ to SharePoint-how to obtain the calm generated by the LINQ Query

Source: Internet
Author: User

We know that LINQ to Sharepoint is actually converted into calm for access to SharePoint. So how can we know what the calm statement we write is eventually converted.
We can use the following methods to achieve our goal.

1. First, create a new class named camldebug in our SharePoint project ,:

Calmdebug. CSCodeAs follows:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. IO;
Using Microsoft. Sharepoint. LINQ;

NamespaceNorthwindlinqtosp
{
Public ClassCamldebug: idisposable
{

PrivateDatacontext _ context;

Public stringwriter writer
{< br> Get ;
private set ;
}

Public camldebug (datacontext context)
{< br> _ context = context;
writer = New stringwriter ();
_ context. log = writer;
}

Public override string tostring ()
{< br> writer. flush ();
return writer. getstringbuilder (). tostring ();
}

Public VoidDispose ()
{
_ 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 =NewCamldebug (DC ))
{
StringQueries = 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 a breakpoint in the code segment to go to debugging (of course, you can also output the calm string saved by queries to any place you want)

 

 

4. Here, some people directly use the following code to output the generated calm directly to the specified TXT file without using the above class for viewing.

VaR Dc = New Northwindentitydatacontext (spcontext. Current. Web. url );
Mycustomers = Dc. getlist <acustomeritem> ( " Acustomer " );
Myorders = Dc. getlist <aordersitem> ( " Aorders " );

Textwriter =NewStreamwriter (@"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 output result of the above Code is as follows:

 

The next task is in yourSpquery

(The following is a reference example 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 = 'apache' showfield = 'productname '/>
<Field name = 'productcontenttypeid' type = 'lookup'
List = 'apache' 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 [ "A orders " ];
var items = List. getitems (query);
foreach (splistitem in items)
{< br> This . listboxoutput. items. add (item [ " title " ] + item [" productproductname " ]);
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.