IT tutorials
 
Technology
 

LINQ to SharePoint and SPMetal : Querying Data Using LINQ to SharePoint (part 2) - Performing a Simple Query

12/2/2013 3:24:12 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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:

 
Others
 
- LINQ to SharePoint and SPMetal : Querying Data Using LINQ to SharePoint (part 1) - Query Limitations
- System Center Configuration Manager 2007 : Client Deployment (part 4) - Client Installation in Image Deployment
- System Center Configuration Manager 2007 : Client Deployment (part 3) - Client Push Installation Wizard
- System Center Configuration Manager 2007 : Client Deployment (part 2) - Manual Installation, Client Push Installation
- System Center Configuration Manager 2007 : Client Deployment (part 1) - Command-Line Properties
- Troubleshooting Exchange Server 2010 : Troubleshooting Client Connectivity (part 2)
- Troubleshooting Exchange Server 2010 : Troubleshooting Client Connectivity (part 1) - Troubleshooting Autodiscover
- Troubleshooting Exchange Server 2010 : Troubleshooting Mail Flow (part 3) - Exchange Mail Flow Troubleshooter
- Troubleshooting Exchange Server 2010 : Troubleshooting Mail Flow (part 2) - Message Tracking
- Troubleshooting Exchange Server 2010 : Troubleshooting Mail Flow (part 1) - Queue Viewer in the EMC
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us