Click here to Skip to main content
15,884,979 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi.

this my modelview .

public class RoleAccess
{
    public int id { get; set; }
    public string name { get; set; }
    public List<ChildRole> children { get; set; }
}



and this is ChildRole :

public class ChildRole
{
    public int id { get; set; }
    public int parentid { get; set; }
    public string name { get; set; }

}


now i need to fill view model with this code :

public List<RoleAccess> SubRole()
{
    List<RoleAccess> sub = new List<RoleAccess>();
    foreach (var parent in Roles.Where(x => x.Id == 0))
    {
        foreach (var child in Roles.Where(x=>x.RoleLevel==parent.Id))
        {
            sub.Select(role=>new RoleAccess
            {
                id = parent.Id,
                name = parent.Description,
                children=child
            });
        }
    }
    return sub;
}



but it show me this error in this line :
children=child


Error :
Cannot implicitly convert type 'StoreFinal.Entities.Entities.Identity.Role' to 'System.Collections.Generic.List<StoreFinal.ViewModel.RoleManagerViewModel.ChildRole>'


What I have tried:

public List<RoleAccess> SubRole()
{
    List<RoleAccess> sub = new List<RoleAccess>();
    foreach (var parent in Roles.Where(x => x.Id == 0))
    {
        foreach (var child in Roles.Where(x=>x.RoleLevel==parent.Id))
        {
            sub.Select(role=>new RoleAccess
            {
                id = parent.Id,
                name = parent.Description,
                children=child
            });
        }
    }
    return sub;
}
Posted
Updated 23-Nov-18 3:13am

Well no - children is a collection of ChildRole objects, and you are trying to assign (what looks like) an instance of a single role to it - that won't work.
Did you mean to say
C#
children.Add(child);
If not, then you need to look at exactly what type a child is - we can't tell from here!
 
Share this answer
 
Comments
kia9372 22-Nov-18 5:01am    
it not show Add Method in for chidren
OriginalGriff 22-Nov-18 5:21am    
Are you sure?
You have declares children as a Generic Collection:

public List<childrole> children { get; set; }

And List always has an Add method!
Unless of course, you have another variable called children in a similar scope?
sub.Select(role=>new RoleAccess
{
    id = parent.Id,
    name = parent.Description,
    children=child
});


"children" is a list and you're trying to assign it a single item. What you need to do is make a list with a single item in it and assign that to children. You do that using code like this;

sub.Select(role=>new RoleAccess
{
    id = parent.Id,
    name = parent.Description,
    children= new List<ChildRole> { child }
});
 
Share this answer
 
Comments
kia9372 22-Nov-18 6:35am    
thanks for answer my question . that error is solved but this code have a problem .

i need to get list of roles and sub role (childrole) .

when i used this code . childrole not go into the main role :

i need return like this :

 *(Role)
       *(childRole)
       *(childRole)
       *(childRole)
 *(Role)
       *(childRole)
       *(childRole)
       *(childRole)

but it show me this with your code : 

 *(Role)
       *(childRole)
*(Role)
       *(childRole)
*(Role)
       *(childRole)
*(Role)
       *(childRole)
*(Role)
       *(childRole)


whats the problem ? how can i solve this ???
F-ES Sitecore 22-Nov-18 7:05am    
You're probably going to have to split this into multiple lines. First get the role, and then check the children property. If it is null then set it to a new List, and then add the child item to it. That way roles with no children get a new list with one item, but roles that already have children get the child added to the existing list.

It looks like you'll need to write code to get the role if it exists too as your code looks like it just creates a new role each time too.
Try something like this:
C#
public List<RoleAccess> SubRole()
{
    return Roles
        .Where(parent => parent.RoleLevel == 0)
        .AsEnumerable()
        .Select(parent => new RoleAccess
        {
            Id = parent.Id,
            Name = parent.Description,
            children = Roles
                .Where(child => child.RoleLevel == parent.Id)
                .AsEnumerable()
                .Select(child => new ChildRole
                {
                    Id = child.Id,
                    ParentId = parent.Id,
                    Name = child.Description,
                })
                .ToList(),
        })
        .ToList();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900