Inefficient Queries with SharePoint 2010 Beta2
Posted
Sunday, February 14, 2010 8:28 PM
by
Itay Shakury
Linq 2 SharePoint distinguishes between three types of queries: Normal Queries, Inefficient Queries, and Unsupported Queries.
For more on that subject see the SDK article: http://msdn.microsoft.com/en-us/library/ee536585(office.14).aspx
According to the SDK (at least as of today), you can execute inefficient queries if you set the AllowInefficientQueries property to true.
Also, If you try to actually try to run an inefficient query, you will get the following exception, that instruct you to set the AllowInefficientQueries flag:
But if you try to to apply this practice to your code, you will soon find out that there’s no property called AllowInefficientQueries at all!
Wondering why?
I’ve done some digging, and found out that it was only present in internal Beta1 distributions, and was removed with the public Beta2. Documentation is probably still outdated.
So if you still want to run inefficient queries, you will have to to take the extra mile and explicitly get the list from SP, then use LINQ 2 Objects to query the list.
Lets say I have 2 lists – Employees, and Departments. I also have a lookup set between them where each employee has a lockup field to the Departments list.
This query should fail due to inefficiency:
IEnumerable<EmployeesContact> res =
from employee in data.Employees
where employee.Department.Budget>1000
select employee;
To make it work, we need to first get the list locally, and work with it offline. The following query should work:
IEnumerable<EmployeesContact> employeesList =
data.Employees.ToList<EmployeesContact>();
IEnumerable<EmployeesContact> res =
from employee in employeesList
where employee.Department.Budget>1000
select employee;
I agree, it’s not the most efficient code, since we actually run tow queries instead of one.. but it’s the only way, and hey - this post is about inefficient queries right..?
-- My name is Itay Shakury, and I’m a SharePoint consultant --