tags:

views:

517

answers:

5

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?

Thanks

UPDATE: Solution for .net v2 please

+3  A: 

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"; });
Greg Beech
Will this work in .net v2?
JL
Thanks Greg, exactly what I wanted to know, is this ok memory wise, to use a delegate?
JL
@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
Yes, memory usage isn't an issue. The GC will clean up the delegate when the method is finished.
Greg Beech
Modified your code to add 'return', since it's a delegate not a lambda
Andreas Grech
Cheers Dreas. I knew something felt wrong...
Greg Beech
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
Lists are just so useful....
JL
Well, feel free to judge for yourself which is ideal for your circumstances: http://dotnetperls.com/dictionary-time
Amber
@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
A: 

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

string value = Configurations.Find(config => config.Name == "myConfig").Value;
A: 

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
+5  A: 

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
A: 

Hi there.

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");
        }

Cheers. Jas.

Jason Evans
Syntax error...
EricSchaefer