i am trying to find all the friends of a user lets imagine the two tables
Users(UserID , Name) FriendShips(id,CreatorID,FriendID,Accepted) CreatorID and FriendID are foreign key of Users table
the query below should return all the users that are "Friends" with the User with UserID= 1; but its returning something else
Int64 userID =1;
ctx.Users.Where(x => x.FriendShips.All(y => y.Accepted == true && y.CreatorID == userID)).ToList();
if someone can tell me what should the query be using the lambda expression i will be grateful.
try flipping the query around
ctx.Friendships.Where(y=>y.CreatorID==userID && y.Accepted==true).Select(y=>y.User).Distinct();
If FriendShips have many Users, the query would be
ctx.Friendships.Where(y=>y.CreatorID==userID && y.Accepted==true).SelectMany(y=>y.Users).Distinct();
Your query returns users whose all friends have the specific id. I don't think this is your intention. Instead, you'd like users whose any friend has the specific id (and has other friends possibly).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.