0

我正在尝试使用 linq to sql 查找不在朋友用户朋友列表中的所有用户。现在我正在将好友列表与系统中的所有用户进行比较。使用 linq to 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();
4

2 回答 2

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;
于 2012-08-23T21:19:12.243 回答
0

您可以使用IEnumerable.Except

例子:

var notIn = allUsersInSystem.Except(friends);

于 2012-08-23T20:53:14.590 回答