在Entity Framework中比较具有多个条件的两个列表

时间:2013-03-21 18:50:28

标签: c# linq-to-entities entity-framework-5

我正在尝试获取User列表FacebookFriend以及当前用户尚未关注的列表(UserFollower)。这是我试图使用的查询,但我仍然得到当前用户已经关注的用户。不知道下一步该做什么。

 var fb = db.Members 
      .Include(i => i.User)
           .Where(x => db.FacebookFriends.Where(y => y.UserID == CurrentUserID)
                .Select(f => f.FacebookID)
                .Contains(x.ProviderUserId)
                && !db.UserFollowers
                .Select(f => f.FollowerID)
                .Contains(x.UserID)
            ).Take(6).ToList();

FacebookIDProviderUserId相同 FollowerIDUserID

这是模特。

public class FacebookFriend
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public string FacebookID { set; get; }
    public string Name { set; get; }

    public User User { get; set; }
}

public class UserFollower
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public int FollowerID { get; set; }
    [ForeignKey("UserID")]
    public User User { get; set; }
    [ForeignKey("FollowerID")]
    public User Follower { get; set; }
}

Member

[Table("webpages_OAuthMembership")]
public class OAuthMembership
{
    [Key, Column(Order = 0), StringLength(30)]
    public string Provider { get; set; }

    [Key, Column(Order = 1), StringLength(100)]
    public string ProviderUserId { get; set; }

    public int UserID { get; set; }

    //public User User { get; set; }
    [Column("UserID"), InverseProperty("OAuthMemberships")]
    public User User { get; set; }
}

1 个答案:

答案 0 :(得分:0)

看起来你的代码应该工作,但为了清楚起见,可以使用查询语法对其进行重组:

var fb =
    (from m in db.Members
     let friendIds = 
         from ff db.FacebookFriends
         where ff.UserID == CurrentUserID
         select ff.FacebookId
     let followerIds = 
         from uf db.UserFollowers
         select uf.FollowerID
     where friendIds.Contains(m.ProviderUserId) && !followerIds.Contains(m.UserID)
     select m)
    .Take(6)
    .ToList();