As described in some MSDN-Forums and by
Eric Pool, Quering data against the context directly via Linq in sandboxed plugins is not possible. You can retrieve the whole DataSet, but filtering is not possible.
If you need to access
and filtering your results from an oData endpoint (also custom ones), you can use the following syntax.
Example OrganizationDataService (with FirstOrDefault):
//Define the Request URI
Guid accountId = new Guid("66BD3ABE-D2CA-E111-9E1F-000C29BDAB09");
string serviceAddress = @"http://crm:5555/DEV/XRMServices/2011/OrganizationData.svc/";
string accountQueryByPrimaryKey = string.Format("AccountSet(guid'{0}')", accountId.ToString());
Uri svcUri = new Uri(serviceAddress);
Uri accountUri = new Uri(string.Format("{0}{1}", serviceAddress, accountQueryByPrimaryKey));
//Create context and credentials ('My' is the name of the CRM organization, these classes are created when you add a service reference to CRM DataService endpoint)
CrmService.MyContext context = new CrmService.MyContext(svcUri);
context.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
//Retrieve data
Account retrievedAccount = context.Execute<Account>(accountUri).FirstOrDefault();
Example Custom Service (with Enumeration):
// Define the Request URI
Uri svcUri = new Uri(@"http://crm:8888/MyDataService/CustomDataService.svc/");
Uri uriAccounts = new Uri(string.Format("{0}{1}", svcUri, "ACCOUNT(3)"));
// Create the context
DataServiceContext context = new DataServiceContext(svcUri)
// Enumerate over the query result.
StringBuilder sb = new StringBuilder();
foreach (ACCOUNT account in context.Execute<ACCOUNT>(uriAccounts)) {
sb.AppendLine("Name: " + account.name);
}
The trick is to use the oData (URI) for Filtering and to interprete just the results.
You can find the MSDN description
here.