I have the class "Group" and inside this class I have a colletion of class "User"
public partial class Group
{
public Group()
{
this.User= new HashSet<User>();
}
[Key]
public int CodGroup { get; set; }
[Required]
[StringLength(50, MinimumLength = 3)]
public string Name { get; set; }
public virtual ICollection<User> User{ get; set; }
}
My class User
public partial class User
{
public User()
{
}
[Key]
public int CodUser { get; set; }
[Required]
public int CodGroup { get; set; }
[Required]
[StringLength(100, MinimumLength = 3)]
public string Name { get; set; }
[Required]
public bool Active{ get; set; }
public virtual Group Group{ get; set; }
}
I Have the collection IEnumerable<Group> Groups
The problem is, my collection of users inside each Group contains users inactive too, How do I take only the users actives in each group using lambda expression???
Groups.Where( ??? )
If you're trying to get groups where any users are active, you want:
var groups = Groups.Where(g => g.Users.Any(u => u.Active));
If you want groups where all the users are active, you want:
var groups = Groups.Where(g => g.Users.All(u => u.Active));
Basically as soon as you express clearly what you want, the LINQ looks pretty similar :)
See more on this question at Stackoverflow