Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working with EF code first. There are two classes which define my many to many association tables:

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

    public virtual ICollection<Habit> Habits { get; set; }
}



public class Habit
{
    [Key]
    public int HabitId { get; set; }

    public virtual ICollection<UserProfile> Users { get; set; }
}

I need to select from my database all habits for current user. I'm very new to c#, and the problem is that I can't understand how to work with complex lambda-expressions. I tried that:

context.Habits.Where(habit => habit.Users
                .Where(user=>user.Email==User.Identity.Name)).ToList()

But it's wrong. Could you please correct my lambda-based query. Thanks.

share|improve this question
1  
user.Email==User.Identity.Name ? –  octref Jul 12 at 17:29
add comment

3 Answers

up vote 2 down vote accepted

Why not add a DbSet<UserProfile> to your context and then do:

context.Users.Include("Habits")
       .First(user => user.Email == User.Identity.Name)
       .Habits;

If you want to avoid the above and rather fix the query you should do:

context.Habits.Where(habit =>                   
           habit.Users.Any(user=>user.Email==User.Identity.Name)).ToList();

Any returns true if any item in the IEnumerable satisfies the condition.

share|improve this answer
add comment

Try using Select (or SelectMany if you want a single flattened list) for the first lambda expression:

context.Habits.Select(habit =>
    habit.Users.Where(user => user.Email == User.Identity.Name)
    ).ToList()

The problem is that Where requires that the lambda expression return a boolean, and it was returning an IEnumerable<UserProfile>.

share|improve this answer
add comment

Because you want to select a specific property (Habits) from the UserProfile entity the most straight-forward way would be to use Select in my opinion:

var habits = context.UserProfiles
    .Where(user => user.Email == User.Identity.Name)
    .Select(user => user.Habits)
    .SingleOrDefault(); // I assume here that the Email is unique
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.