cgipson
cgipson

Reputation: 410

Dynamics 365 OrganizationServiceContext Linq Query with multiple Joins

I am running into an issue running most of my queries. I can generate a link query with a join to only one related entity at a time. But when I run a LINQ query with multiple joins as in the example below, I get the "Sequence contains no elements" error.

var query =
(
    from permission in context.CreateQuery<ServiceModel.Types.idoe_permission>()
        join contact in context.CreateQuery<ServiceModel.Types.Contact>()
            on permission.idoe_contact_idoe_permission.Id equals contact.Id
        join corporation in context.CreateQuery<ServiceModel.Types.idoe_corporation>()
            on permission.idoe_idoe_corporation_idoe_permission.Id equals corporation.Id
        join role in context.CreateQuery<ServiceModel.Types.idoe_role>()
            on permission.idoe_idoe_role_idoe_permission.Id equals role.Id
    where contact.idoe_ADB2CID == request.UserId
    select new { Corporation = corporation, Role = role }
).ToList();

I am only able to "join" one entity at a time. Other examples I have seen allow multiple joins, but I have not been able to get this to work.

Any suggestions?

Upvotes: 0

Views: 1569

Answers (1)

Aron
Aron

Reputation: 3935

It looks like you're using the relationship names rather than the lookup field names in the joins. Using the lookup field names instead might look something like this:

var query = (from permission in context.CreateQuery<ServiceModel.Types.idoe_permission>()
         join contact in context.CreateQuery<ServiceModel.Types.Contact>() on permission.idoe_contactid.Id equals contact.Id
         join corporation in context.CreateQuery<ServiceModel.Types.idoe_corporation>() on permission.idoe_corporationid.Id equals corporation.Id
         join role in context.CreateQuery<ServiceModel.Types.idoe_role>() on permission.idoe_roleid.Id equals role.Id
         where contact.idoe_ADB2CID == request.UserId
         select new { Corporation = corporation, Role = role }).ToList() ;

Upvotes: 1

Related Questions