2. Performing a Simple Query
Now that I’ve discussed the dos and don’ts
of LINQ to SharePoint, let’s get started on creating a few queries
using our sample application.
We’ll extend our user interface to make it easier to
view our results and the CAML that’s being generated behind the scenes.
On Form1.cs in LinqSampleApplication, add a SplitContainer under the
buttons that we added earlier. Set it to anchor to all sides. Within
the left panel, drop a WebBrowser control, and set its Dock property to
Fill. In the right panel, drop a DataGridView control, and set its Dock
property to Fill. Add another button next to those created earlier, and
label the button Basic Query. Once you’ve finished, your form should look like this:
In the on-click event handler for the Basic Query button, add the following code:
private void button6_Click(object sender, EventArgs e)
{
using(HireSampleDataContext dxRead = new HireSampleDataContext(SiteUrl.Text))
{
//create a stringbuilder to store the CAML query
StringBuilder sb = new StringBuilder();
using (StringWriter logWriter = new StringWriter(sb))
{
//log the generated CAML query to a StringWriter
dxRead.Log = logWriter;
//Since we're only reading data, disabling object tracking
//will improve performance
dxRead.ObjectTrackingEnabled = false;
var basicQuery = from c in dxRead.HireContracts
where c.ContractStartDate.Value < DateTime.Now
orderby c.ContractStartDate
select c;
//DataGridView can't bind to IEnumerable. Calling ToList
//executes the query and returns a generic List
dataGridView1.DataSource = basicQuery.ToList();
}
//create a temporary file for the generated CAML
string fileName = Path.Combine(Path.GetTempPath(), "tmpCaml.xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
doc.Save(fileName);
//point the browser control to the temporary generated CAML file
webBrowser1.Navigate(fileName);
}
}
Notice a few interesting things about this code sample. First, notice the use of the Log property on the DataContext object. When a TextWriter object is assigned to this property, the CAML that is generated by the LINQ provider will be output to the TextWriter
when it is executed. In this sample, we’ve made use of that
functionality to generate a temporary XML file that we can view using
our WebBrowser control.
Another thing to highlight is the ObjectTrackingEnabled flag: since we’re planning to execute queries only using this DataContext, setting this flag to false will improve performance, because the provider doesn’t need to track references to the underlying SPListItem objects that are represented by the result set.
Finally, the last thing to note is the use of the ToList extension method when assigning the query as the data source for our DataGridView. LINQ queries are not actually executed until the result set is enumerated. Since the DataGridView control doesn’t support the IEnumerable interface for data sources, the result set is never enumerated, and therefore the query is never executed. The ToList extension method enumerates the result set to convert the results into a generic list; this list is then passed to the DataGridView, enabling us to view the results in the user interface.
Running the sample application and then clicking the Basic Query button should yield the following result: