The data structure is as follows:
user (identifier)
UserApp (user_id, app_id)
UserSkill (user_id, skill_id)
Using linq-to-sql or EF, how would I build a query to gracefully return only users who have every requested application and skills?
Also, how could I customize the query to return any user who has at least one of the requested applications or skills? Essentially OR vs AND (above).
UPDATE 1:
Therefore, I think we are close. Basically, I only want to return users who have ALL the required applications and skills. If we have two arrays of requested identifiers for skills and applications:
int[] requestedAppIDs
I would only like to return the user if they have applications 1,2,3 and skills 4,5,6.
var usersWithAllSelectedAppsAndSkills = context.Users .GroupJoin(context.UserApp, k => k.id, k => k.user_id, (o, i) => new { User = o, UserApps = i }) .GroupJoin(context.UserSkill, k => k.User.id, k => k.user_id, (o, i) => new { User = o.User, o.UserApps, UserSkills = i }) .Where(w => !requestedAppIDs.Except(w.UserApps.Select(x => x.app_id).ToArray()).Any() && !requestedSkillIDs.Except(w.UserSkills.Select(x => x.skill_id).ToArray()).Any()) .Select(s => s.User) .ToList();
Obviously, LINQ does not know how to translate UserSkills.Select (). ToArray () in my Where () in SQL. How can i do this?
And secondly, the OR solution (the user has any of the requested applications or skills).
c # linq-to-sql entity-framework
bmherold
source share