I have a getvalue
object that contains a price list which consists of 5 items. I need to get the value of one of the elements. I can get the value by index:
You can either change your array to Dictionary<string, yourType>
or use LINQ to perform linear search for your object by name:
return getValue1.ValuationPrices.First(x => x.Name == "myName").Value.ToString();
The screenshot helped quite a bit... people can't guess what your classes look like internally. Your comment to Marcin indicates that PriceType
might be an enumeration. So assuming:
PriceType
is actually an enum, not a stringPriceType
you're searching for is guaranteed to be in that collection at once and one time onlyThis should work:
return getValue1.ValuationProces.Single(x => x.PriceType == PriceType.WholeSale).Value.ToString();
This is basically the same as Marcin's - if I'm right about PriceType
being an enum and this works, then you should just accept his answer and move on.
You could do this by adding an indexer property to the ValuationPrices
type.
public ValuationPrice this[string name]
{
get
{
return this.First(n => n.Name == value);
}
}
Then you would be able to write getvalue1.ValuationPrices["fieldName"]
.
The implementation of the indexer property will vary depending on the internal structure of your classes, but hopefully this gives you some idea of the syntax used to implement the indexer.