10

I have the following list item

public List<Configuration> Configurations
{
    get;
    set;
}

 public class Configuration
  {
    public string Name
     {
       get;
       set;
     }
    public string Value
      {
       get;
       set;
     }
 }

How can I pull an item in configuration where name = value?

For example: lets say I have 100 configuration objects in that list.

How can I get : Configurations.name["myConfig"]

Something like that?

UPDATE: Solution for .net v2 please

John Saunders
  • 160,644
  • 26
  • 247
  • 397
JL.
  • 78,954
  • 126
  • 311
  • 459

5 Answers5

24

Using the List<T>.Find method in C# 3.0:

var config = Configurations.Find(item => item.Name == "myConfig");

In C# 2.0 / .NET 2.0 you can use something like the following (syntax could be slightly off as I haven't written delegates in this way in quite a long time...):

Configuration config = Configurations.Find(
    delegate(Configuration item) { return item.Name == "myConfig"; });
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
  • Thanks Greg, exactly what I wanted to know, is this ok memory wise, to use a delegate? – JL. Aug 01 '09 at 11:49
  • @JL - The original answer requires C#3.0/.NET3.5 but I've added one that will work with C#2.0/.NET2.0 – Greg Beech Aug 01 '09 at 11:49
  • Yes, memory usage isn't an issue. The GC will clean up the delegate when the method is finished. – Greg Beech Aug 01 '09 at 11:50
  • Modified your code to add 'return', since it's a delegate not a lambda – Andreas Grech Aug 01 '09 at 11:50
  • Just pointing out here, while memory usage is fine, as I mentioned in my separate answer below, you'll get much better performance on lookup operations if you use a Dictionary instead of a List. – Amber Aug 01 '09 at 11:52
  • Well, feel free to judge for yourself which is ideal for your circumstances: http://dotnetperls.com/dictionary-time – Amber Aug 01 '09 at 11:59
  • @Dav - Indeed, if everything is accessed randomly then a dictionary would make sense for sure. I'd just assumed that this was an occasional find operation rather than the normal case. But if this is the normally intended usage then yes a dictionary would be better for sure as it's typical O(1) lookup whereas this approach is O(n) [though for small n there's probably not much difference] – Greg Beech Aug 01 '09 at 14:27
6

It seems like what you really want is a Dictionary (http://msdn.microsoft.com/en-us/library/xfhwa508.aspx).

Dictionaries are specifically designed to map key-value pairs and will give you much better performance for lookups than a List would.

Amber
  • 507,862
  • 82
  • 626
  • 550
3

Consider using a Dictionary, but if not:


You question wasn't fully clear to me, one of both should be your answer.

using Linq:

var selected = Configurations.Where(conf => conf.Name == "Value");

or

var selected = Configurations.Where(conf => conf.Name == conf.Value);

If you want it in a list:

List<Configuration> selected = Configurations
    .Where(conf => conf.Name == "Value").ToList();

or

List<Configuration> selected = Configurations
    .Where(conf => conf.Name == conf.Value).ToList();
Dykam
  • 10,190
  • 4
  • 27
  • 32
0

Try List(T).Find (C# 3.0):

string value = Configurations.Find(config => config.Name == "myConfig").Value;
weiqure
  • 3,247
  • 2
  • 27
  • 31
0

Here's one way you could use:

static void Main(string[] args)
        {
            Configuration c = new Configuration();
            Configuration d = new Configuration();
            Configuration e = new Configuration();

            d.Name = "Test";
            e.Name = "Test 23";

            c.Configurations = new List<Configuration>();

            c.Configurations.Add(d);
            c.Configurations.Add(e);

            Configuration t = c.Configurations.Find(g => g.Name == "Test");
        }
Jason Evans
  • 28,906
  • 14
  • 90
  • 154