I am having an issue getting my LINQ query to output as I would like. I am unsure I am taking the right approach.
Tables:
I have two tables Contacts
and Permissions
with which I perform a LEFT OUTER JOIN.
Join Query:
from contact in Contacts
join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
from p in permissionGrp.DefaultIfEmpty()
where (p==null && isAllowed) || (p!=null && /* ... further conditions */))
orderby /* ... ordering removed */
select new { contact, permission = p };
This matches the permissions to a contact where applicable, and null
when no matching permission exists.
Desired
I don't wish to have duplicate contacts, I am only interested in the first Contact-Permission record. Like so:
Attempt:
So I assumed that I need to Group By
my contact.Id
and somehow select FirstOrDefault()
on the permissions collection.
from contact in Contacts
join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
from p in permissionGrp.DefaultIfEmpty()
where (p==null && isAllowed) || (p!=null && /* ... further conditions */))
orderby /* ... ordering removed */
group p by contact into contactPermissionsGrp
select new { contact = contactPermissionsGrp.Key, permission = contactPermissions.FirstOrDefault() };
Result:
Unfortunately this results in a NotSupportedException: Specific method is not supported.
. But I am not sure if my approach is correct or a limitation of the LightSpeed ORM.
Any advise would be appreciated.