Home
Manage Your Code
Snippet: List.Find() (C#)
Title: List.Find() Language: C#
Description: How to find objects in Generic Lists, e.g List<Application> apps.Find(p.Name == parameter) Views: 608
Author: Zoran Donev Date Added: 10/28/2011
Copy Code  
1    public class Application
2    {
3        public Application(string name)
4        {
5            this.name = name;
6        }
7
8        private string name;
9
10        public string Name
11        {
12            get { return name; }
13        }
14
15        private List<Resource> resources;
16
17        public  List<Resource> Resources
18        {
19            get { return resources; }
20            set { resources = value; }
21        }
22
23        public void AddResource (Resource resource)
24        {
25            resources.Add(resource);
26        }
27
28        public bool Equals(Application res)
29        {
30            if (this.Name == res.Name)
31            {
32                return true;
33            }
34            else
35            {
36                return false;
37            }
38        }
39
40    }
Usage
public List GetApplicationResources(string applicationName)
        {
            if (!apps.Contains(new Application(applicationName))
            {
                ApplicationBL appBL = new ApplicationBL(this.Server, this.Database);
                appBL.Log += new LogEventHandler(Log);
                Application app = new Application(applicationName); 
                app.Resources = appBL.ListApplicationResources(applicationName, null); 
            }


            return apps.Find(p => p.Name == applicationName).Resources;
            
        }