I have the following model. Each Module has a nested collection of children of type module. Each module also has a collection of Permissions.
[DataContract(IsReference = true)]
public class Module
{
[Key]
[DataMember]
public Guid ModuleId { get; set; }
[Required]
[StringLength(100)]
[DataMember]
public string Title { get; set; }
[StringLength(100)]
[DataMember]
public string Description { get; set; }
[StringLength(50)]
[DataMember]
public string Icon { get; set; }
[Required]
[RegularExpression(@"[^\s]+")]
[StringLength(50)]
[DataMember]
public string Route { get; set; }
[DataMember]
public ICollection<Permission> Permissions { get; set; }
[DataMember]
public Guid? ParentModuleId { get; set; }
[ForeignKey("ParentModuleId")]
[DataMember]
public virtual ICollection<Module> Children { get; set; }
}
[DataContract(IsReference = true)]
public class Permission
{
[Key]
[DataMember]
public Guid PermissionId { get; set; }
[Required]
[StringLength(100)]
[DataMember]
public string Role { get; set; }
[DataMember]
public Guid ModuleId { get; set; }
[ForeignKey("ModuleId")]
[DataMember]
public Module Module { get; set; }
}
I have a Query All function as below, which would return correctly all root with its children.
public override IQueryable<Module> All()
{
return this.Context.Set<Module>().Include(c => c.Children).Where(p => p.ParentModuleId == null);
}
Now, I want to return same list of root with its children which has the Permission "User". How do i do this. This is what i have so far. Is this correct way of doing this? please help.
return this.Context.Set<Module>().Include(c => c.Children).Where(p => p.ParentModuleId == null).Include(p => p.Permissions).Where(s => s.Permissions.Any(r=>r.Role=="User"));
btw, i have no idea how to use properly these functions such as include,where,any,select many functions. Any tutorials or books for this are appreciated. I can't find any good tutorial about this since i don't know what keyword to search for. Was this EF or LINQ.