Need Help Converting to Linq

Posts   
 
    
KristianP
User
Posts: 32
Joined: 23-Feb-2005
# Posted on: 14-Dec-2009 23:08:05   

Anyone know how use linq for the following loop:

    public bool CanNominate(NominationTypeEntity nominationType)
    {
        // admins are able to nominate.
        if (userContext.IsAdministrator) return true;

        // if no roles are attached, everyone can nominate.
        if (nominationType.NominationType_Role.Count == 0) return true;

        // todo: refactor to linq expression.
        foreach (User_Group_RoleEntity ugr in userContext.UserGroupRoles)
        {
            foreach (NominationType_RoleEntity e in nominationType.NominationType_Role)
            {
                if (e.RoleID == ugr.RoleID)
                {
                    return true;
                }
            }
        }

        return false;
    }

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Dec-2009 05:03:48   

If I understand your code, both nominationType and userContext.UserGroupRoles are already fetched so you have the data in-memory. If that is true, you don't need LINQ2LLBL, just Linq2Objects. So you can do something like:

bool toReturn = nominationType.NominationType_Role.Where(ntr => 
     userContext.UserGroupRoles.Select(ugr => ugr.RoleId)
          .Contains(ntr.RoleId)).Count() > 0;
David Elizondo | LLBLGen Support Team