Combining LINQ to SharePoint and LINQ to Objects Share Point 2010

Although the join syntax in LINQ to SharePoint is very powerful, in some situations you won’t be able to retrieve the data that you need using this syntax. Some operations that you require are not permitted on the LINQ to SharePoint provider, because they are considered inefficient.

Performing an In-Memory Subquery
Here’s an example of an in-memory subquery: Suppose that our sample application requires a list of asset tags and locations codes for a particular contract, but only where the locations are also being used to store assets that are subject to another contract. If you were writing this query using SQL, it would be relatively straightforward—something along the lines of this:

OK—maybe this is not that straightforward with all the joins, but you get the picture. You could use a subquery to filter the results to suit your requirements. Unsurprisingly, given its similarity to SQL, LINQ syntax also supports a similar operation. Let’s use our sample application to try it out. As usual, add a new button, label it Sub Query and add the following code:

In this code sample, we’ve defined the subquery, and then used it within the main query. I’ve split up the queries for the sake of clarity; LINQ syntax allows you to combine them within a single query if required.

When you run this query using the sample application, an exception will be thrown, because the LINQ to SharePoint parser can’t convert the statement into CAML, because the CAML syntax doesn’t support subqueries. However, it is still possible to execute this query by making a small modification. Modify the subquery declaration to render the results to a List, as follows:

This time, clicking the Sub Query button will return the expected result set, and an examination of the generated CAML queries will reveal that two queries were generated. The first query corresponds to the subquery and the second corresponds to the main query without the inclusion of the subquery. So how does this work? Using ToList in the definition of the subquery forces the query to be executed immediately, returning the results as a generic list. The generic List object implements IEnumerable<T> and can therefore be used within a LINQ expression. The main LINQ query then performs the subquery using LINQ to Objects as opposed to LINQ to SharePoint, yielding the expected results. In effect, adding ToList to a query allows you to process the results using the full power of LINQ to Objects. However, as discussed earlier, this approach has drawbacks, and efficiency must be given serious thought before you adopt this technique.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Share Point 2010 Topics