Solved

How to filter generic lists by different algorithms or filter criteria?

Posted on 2010-11-09
3
697 Views
Last Modified: 2013-12-17
We have a situation that extends beyond normal security/permissions where in addition to determining if users have access to certain areas of an application with security principles and identities we need to filter results within sections of the application based on additional values.  Is there a generic way to filter values in a generic list using a different function (e.g. predicate delegates or something similar)?

Example:  Imagine that an application determines if user’s have access to a customer editing screen, but then additionally needs to determine which Customers the user has access to view based on a variety of criteria (e.g. by the Company the customer belongs to, by the Region, by the Customer Level, etc.).  Then imagine that on another section of the application you need to do this same type of filtering of the data that the user can see or edit using different objects and criteria and the filtering cannot be done in the WHERE clause of select statements (e.g. many many pre-existing stored procs that do not use this model).

Take these for example: Filter a List<Person> by peole with the Name == 'John' .  Filter a List<Customer> by customers that have any of the following companies listed for the Company value "Microsoft", "Apple", "Dell" and their Customer Tier is == 2.

  public class Person
    {
        public Person()
        {
            Id = Guid.NewGuid();
        }

        public string Name { get; set; }
        public Guid Id { get; set; }
    }


public class Customer : Person
    {
        public string Company { get; set; }
	public string Region { get; set; }
	public int Tier { get; set; }
    }

Open in new window

0
Comment
Question by:endrec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34096801
You can use the FindAll() method of a generic List<> to return a new list filtered based on a delegate.
0
 
LVL 16

Accepted Solution

by:
BurnieP earned 500 total points
ID: 34098217
You can use lambda expression to filter your results :


List<Person> oPersonList = GetPersonList();
oPersonList = oPersonList .Where(c => c.Name == "John").ToList();

List<Customer> oCustomerList = GetCustomerList();
oCustomerList = oCustomerList.Where((c => c.Company == "Microsoft" || c.Company == "Apple" || c.Company == "Dell") && c.CustomerTier == 2)).ToList();

Open in new window

0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34101607
There are different functions to short/ find particular element from List. l.e Select, Find, FindAll etc.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question