2

In English I'm trying to: "Return a list of People who don't have the TeamID in their List of Teams

I have a many-to-many relationship between a Person table and a Team table so each instance of a Person has a List of Teams that they are a member of. (each Team also has a list of People)

Table

here a Person 2 would have a list of two Team objects with ID's of 1 and 2

I'm trying to write a query that, if TeamID were 18 it would return only Persons 1 and 3 for example

This is my attempt:

var query = from p in db.People
            where( query2 = from t in p.Teams
                            where t.ID != teamID
                            select t)
            select p;
4

2 回答 2

6

使用基于方法的 LINQ 语法,这变为:

var query = db.People.Where(p => !p.Teams.Any(t => t.ID == 18));
于 2013-06-14T11:05:58.113 回答
0

适应您的代码:

var query =    
        from p in db.People
        where !(from t in db.Teams    
                select t.ID)    
               .Contains(p.ID)    
        select p;
于 2013-06-14T11:10:07.887 回答