Tyler Durden
Tyler Durden

Reputation: 2181

Getting a foreign key in a table

In my website, a user can add foreignExpressions to his account. The user model looks like this:

   [Table("UserProfile")]
   public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }

    public List<ForeignExpression> learnedexpressions { get; set; }
}

Then in the controller I'd like to get the current user's stored expressions like this:

db.ForeignExpressions.Select(f => f.UserId == Membership.GetUser().ProviderUserKey);

However, ForeignExpression does not contain a UserId field, so there's no visible UserId for f. But the UserProfile has a collecion of ForeignExpressions, so I have a UserId field in the ForeignExpression table. I'm confused, how am I supposed to get the ForeignExpressions for my user?

Edit:

    public class ForeignExpression
{
    public int ID { get; set; }

    public string expression { get; set; }
    public string context { get; set; }
    public string meaning { get; set; }
    public DateTime dateAdded { get; set; }


}

Upvotes: 0

Views: 73

Answers (1)

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19261

int userID = Membership.GetUser().ProviderUserKey;

db.UserProfiles.Find(q => q.UserID == userID).LearnedExpressions.ToList(); 

Upvotes: 1

Related Questions