0

I have three tables viz: Person(PersonID(INT), Personname(varchar)) , Items(ItemID(INT), Itemname(varchar)) and PersonItemAssoc(PersonItemAssoc(Int), PersonID(INT), ItemID(INT)).

PersonItemAssoc is having many to many association for personid and Itemid.

I want to get way in which if I pass the itemId, I should get all the PersonIds which dont have an association witn this ItemId in the PersonItemAssoc table.

I am using Entity Framework 4.0.
Please suggest a way for implementing this.

1 Answer 1

2
var peopleWithoutItem = from p in Context.Person
                        where !p.PersonItems.Any(pi => pi.Item.ItemId == someItemId);
                        select p;

Note that if you get rid of PersonItemAssoc(int) and make the PersonItemAssoc PK the compound of PersonID and ItemID then the EF can do People to Items as a many to many, instead of two 1 to many relationships.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, You are correct, but I have PersonItemAssoc(int) used as FK in other tables, due to which I cant drop that one.

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.