1

I have a list of recipients to send a message, but I want to exclude those from a certain Role, the problem is that I have this:

var recipients = (from up in _db.UserProfiles
                  join ms in _db.MembershipEntities
                      on up.UserId equals ms.UserId
                  join st in _db.Studios
                      on up.StudioId equals st.StudioId
                  select new
                  {
                      up.UserId,
                      up.UserName,
                      up.StudioId,
                      st.Name,
                      ms.Roles
                  }).ToList();

And the result is what I want, only that the query returns an object Role which has a RoleID and a RoleName, so Im not sure how to select the RoleName so I can specify which role I want to exclude.

4
  • Could you give us a small sample set (input, output, wanted output, why)? Commented Nov 27, 2013 at 18:04
  • You should start by editing your scrollbar away my friend. Commented Nov 27, 2013 at 18:06
  • Why not exclude it in the join? Commented Nov 27, 2013 at 18:06
  • Do you want to only return users from the database if they have a given role or would you rather return all users to memory and then filter based on a given role? Commented Nov 27, 2013 at 18:07

1 Answer 1

1

You could add a where clause to the query which filters the users within a certain role:

where ms.Roles.All(role => role.RoleName != "<insert role name>")

Your complete query will look like this:

(from up in _db.UserProfiles
    join ms in _db.MembershipEntities on up.UserId equals ms.UserId
    where ms.Roles.All(role => role.RoleName != "<insert role name>")
    join st in _db.Studios on up.StudioId equals st.StudioId
    select new { up.UserId, up.UserName, up.StudioId, st.Name, ms.Roles})
    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.