Skip to content

Advanced querying across related entities

OData can also be used to querying for results across related entities.

The example below illustrates how to retrieve all contacts that are party (with a specific role) on an open case (File) where the case has a specific free information (FileInfo) associated with it.

Example:

string partyRole = "Sagspart"; // The role that the party on the file must have
string infoType = "Belægning"; // The type of information that the file must have
string infoValue = "AF2"; // The value of information that the file information must have
var query = from c in context.Contacts
where c.Files.Any(fc =>
fc.CustomLabel_Value == partyRole &&
fc.File.Closed == null &&
fc.File.Infoes.Any(fi =>
fi.CustomLabel_Value == infoType &&
fi.Info_Value == infoValue))
select new Contact
{
ID = c.ID,
Summary = c.Summary
};
// Executing the query
var contacts = new DataServiceCollection<Contact>(query);
// Paging thru the results to get them all
while (contacts.Continuation != null)
{
contacts.Load(context.Execute(contacts.Continuation));
}
// Now we have all contacts that are party of an open cases with role partyRole and the case have an information of type infoType with value infoValue
foreach (Contact c in contacts)
Console.WriteLine(c.ID + " - " + c.Summary);