I’ve been using Linq for about a year and I have only a rudemntary understanding of SQL. So the following problem might be trite, and I appologize for that now. But at work I’ve been asked to figure out the most performant way to use linq on the following sql. and I need some help.
I have a Client table and a ClientParticiaption table. The ClientParticiaption table identifies Clients that particpate in a special training program. I need to determine which Client’s are not participating in the special training program. Here’s the SQL that works:
SELECT *
FROM Client
WHERE ClientControlID NOT IN
(
SELECT ClientControlID
FROM ClientParticipation
)
I’m having trouble converting this to its equivalent linq statement. I think I should be using the Except operator but I’m not sure how in this case. Any thoughts would be very helpful.
Here’s what I’m currently doing, which works, but I suspect this is not very effecient. Note, I’m using Entity Framework and I may have incorrectly assumed that resolving this issue has nothing to do with EF.
IList<int> clientControlIDs =
ClientArngmt.Select(
cac => cac.ClientControlID ).ToList();
IList<int> particiaptionClientArngmtIDs =
Participations.Select(
cap => cap.ClientArngmtID ).ToList();
IEnumerable<int> notParticipatingClientArngmtIDs =
clientArngmtIDs.Except(
particiaptionClientArngmtIDs );
1st solution:
2nd solution: