Our users need to filter through a list of groups from one list to bring in the groups that should have access on another. The one list is overwhelming, since a group may be created on the fly for special purposes at any time. But the list of groups needed for this secondary application may be quite small in comparison.
But each users’ organization will have a different beginning and ending group list. So filtering criteria will be different for each. Hence, to provide that capability, I need to be able to create filters for these lists dynamically.
Looking for a solution, I ran across this article that I thought would work. But it broke down when I tried to use more than one ‘where’ statement.
The solution I did use may not be elegant. And I’m hoping it can be made more so, yet more flexible, in the future. But it works. So I wanted to pass it on, in case someone else could use it:
public static ListGetFilteredRoles(List roleFilterList) { EntityObject context = new EntityObject(); IQueryable matches = context.Roles; List integers = new List (); IEnumerable filterSet = from r in context.Roles select r; // now figure out a filter foreach (GroupFilterCLR filter in roleFilterList) { String strTmp = filter.Regex; List tmp = new List (); if (filter.Status == FilterStatus.INCLUDE) { if (filter.Relation == FilterRelation.CONTAINS) { tmp = (from r in context.Roles where r.Name.Contains(strTmp) select r).ToList (); } else if (filter.Relation == FilterRelation.STARTS_WITH) { tmp = (from r in context.Roles where r.Name.StartsWith(strTmp) select r).ToList (); } else if (filter.Relation == FilterRelation.ENDS_WITH) { tmp = (from r in context.Roles where r.Name.EndsWith(strTmp) select r).ToList (); } } else { if (filter.Relation == FilterRelation.CONTAINS) { tmp = (from r in context.Roles where !r.Name.Contains(strTmp) select r).ToList (); } else if (filter.Relation == FilterRelation.STARTS_WITH) { tmp = (from r in context.Roles where !r.Name.StartsWith(strTmp) select r).ToList (); } else if (filter.Relation == FilterRelation.ENDS_WITH) { tmp = (from r in context.Roles where !r.Name.EndsWith(strTmp) select r).ToList (); } } filterSet = filterSet.Intersect(tmp); } return Mapper.Map<List<Role>, List<RoleDTO>>(filterSet.ToList ()); }
Notice, too, that it shows how to negate an expression.
