Neo
Neo

Reputation: 16219

How to get value from IEnumerable collection using its Key?

I have IEnumerable collection like following

IEnumerable<Customer> items = new Customer[] 
{ 
     new Customer { Name = "test1", Id = 999 }, 
     new Customer { Name = "test2", Id = 989 } 
};

I want to get value using key Id

I tried like following

public int GetValue(IEnumerable<T> items,string propertyName)
{
      for (int i = 0; i < items.Count(); i++)
      {
           (typeof(T).GetType().GetProperty(propertyName).GetValue(typeof(T), null));
           // I will pass propertyName as Id and want all Id propperty values 
           // from items collection one by one.
      }
}

Upvotes: 11

Views: 132200

Answers (6)

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Using LINQ you can get all customers names (values) having specific value in this way:

var valuesList = items.Where(x => x.Something == myVar).Select(v => v.Name).ToList();

For single customer name you can do this:

var singleName = items.FirstOrDefault(x => x.Id == 1)?.Name;

Obviously, the Id can be 1, 2 or any other.

Edit:

I recommend you List<Customer> instead of Customer[]

So,

var items = new List<Customer> 
{ 
     new Customer { Name = "test1", Id = 999 }, 
     new Customer { Name = "test2", Id = 989 } 
};

Upvotes: 14

Igor
Igor

Reputation: 55

private TextBox [] Collectionstextboxonpanel(Panel panel)
{

    var textBoxspanel1 = panel.Controls.OfType<TextBox>(); // select controls on panle1 by type

    IEnumerable<TextBox> textBoxes = textBoxspanel1; // create collection if need 
    TextBox[] textBoxes1 = textBoxes.ToArray(); // Array collection
    return textBoxes1;                         // get back TextBox Collection
}

Upvotes: -2

w.b
w.b

Reputation: 11228

If you want to retrieve a Customer name from a collection by its Id:

public string GetCustomerName(IEnumerable<Customer> customers, int id)
{
    return customers.First(c => c.Id == id).Name;
}

Upvotes: 16

Matthew Watson
Matthew Watson

Reputation: 109547

Do you want to look things up repeatedly after creating the list? If so, you might want to consider creating a dictionary to do the lookups, like so:

IEnumerable<Customer> items = new Customer[]
{
    new Customer {Name = "test1", Id = 999},
    new Customer {Name = "test2", Id = 989}
};

var lookup = items.ToDictionary(itemKeySelector => itemKeySelector.Id);

var result = lookup[989];

Console.WriteLine(result.Name); // Prints "test2".

I'm assuming that you don't create the collection in the first place - if you had control over creating the original collection you could use a dictionary in the first place.

Upvotes: 4

Eser
Eser

Reputation: 12546

// I will pass propertyName as Id and want all Id propperty values

// from items collection one by one.

If I understand you correctly

public static IEnumerable<object> GetValues<T>(IEnumerable<T> items, string propertyName)
{
    Type type = typeof(T);
    var prop = type.GetProperty(propertyName);
    foreach (var item in items)
        yield return prop.GetValue(item, null);
}

Upvotes: 12

Nadeem Khoury
Nadeem Khoury

Reputation: 937

Just use LINQ to achieve what you want to do. if you want to retrieve a specific value you can use where like this:

public Customer GetCustomerById(IEnumerable<Customer> items,int key)
{
    return items.Where(x=>x.id==key)
   .Select(x =>x.Name)
   .First(); 
}

this will retrieve the customer who match a specific Id.

Upvotes: 8

Related Questions