2012-08-23 47 views
0

我試圖找到所有的用戶不在朋友用戶朋友列表使用LINQ到SQL。現在我正在將friendslist與系統中的所有用戶進行比較。 Theres得到更好的方式使用linq to sql來做到這一點。感謝您的幫助LINQ到SQL不在

// Comparing this to list to see who does not exist. 
// What I would like to do is just use one statement 
// to get the list of non friend users 
var friends = (from x in db.FriendsLists 
       where 
        (x.TheUser == GlobalVariables.User.ID) || 
        (x.TheFriend == GlobalVariables.User.ID) 
       select x).ToList(); 

var allUsersInSystem = (from x in db.Users select x).ToList(); 
+0

可能重複的[ 「NOT IN」 子句中LINQ到實體(http://stackoverflow.com/questions/432954/not-in-clause- in-linq-to-entities) – devuxer

+0

實際上,這個問題是關於LINQ to Entities,而不是LINQ to SQL。這可能會在LINQ to SQL中起作用... – devuxer

回答

2
var friends = (from x in db.FriendsLists 
            where 
             (x.TheUser == GlobalVariables.User.ID) || 
             (x.TheFriend == GlobalVariables.User.ID) 
            select x.UserID).ToList(); 

    var notInFriendList = from nf in db.Users 
       where !friends.Contains(nf.UserID) 
       select nf;