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.
}
}
items.Select(x=>x.Id)
would do it. what are you trying to achieve? – MucilaginouspropertyName
argument and then use that in the linq where clause. Same with what you need to select. If it's the wholeT
item, you don't need a specificselect
but if you need just a specific property, pass another selector function (of typeFunc<T, whatever type>
) – Russi