开发者

LINQ to Entities projections

UPDATE: I essentially want to get a list of all permissions and a list of all permissions for a given role. I then want to mark each permission in a list of SelectListItems as selected when they already belong to the Role

I have three tables:

Project 1-* Role - Permission

I am trying to create and IEnumerable using projection that has all permissions in the list but when I project to SelectItemList it identifies the Permissions that are already associated with a particular role:

public IEnumerable GetAllPermissionsPLusRole(int projectid, int roleid)
    {
        using (var db = new Entities())
        {
            var permissions = (from p in db.Permissions
                               select p).ToList();

            var permissionsForRole = from r in db.Roles
                                     where r.RoleId == roleid && r.PrjectProjectId == contractid
                                     select r.Permiss开发者_StackOverflowions.ToList();

            IEnumerable<SelectListItem> selectList = from p in permissions select new SelectListItem { Text = p.PermissionName, Value = p.PermissionId.ToString()};
            return selectList;
        }
    }

What I want to do is have a conditional Selected in the projection so when I display the list I can show the permissions already associated to the Role.

Thanks in advance.


If I understand it right, you've already fetched all the data, now you just need to check if given permission is in the permissionsForRole list, i.e. like that:

IEnumerable<SelectListItem> selectList = from p in permissions
    select new SelectListItem
    {
        Text = p.PermissionName, 
        Value = p.PermissionId.ToString(),
        Selected = permissionsForRole.Contains(p)
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜