I am trying to access the contents of an IEnumerable by string rather than int.
METHOD
public List<Foo> GetFoo(IEnumerable<Bar> bar)
{
List<Foo> foo = new List<Foo>();
var query = from x in bar
select new Foo()
{
foo = x.foo,
bar = x.bar
};
foo = query.ToList();
return foo;
}
VIEW
<td>@foo["bar"].foo<td>
I know the above doesn't exist, but that's what i'm looking to do. If i do foo[0].foo
it --obviously-- works.
EDIT
I went with the Dictionary<string,Foo>
approach as @D Stanley and @juharr recommended, but i still have to iterate through these results via a foreach loop in order to access the KeyValuePair. I'm trying to bypass a foreach and access the results just via the Key. Is this possible?
public Dictionary<string,Foo> GetFoo(IEnumerable<Bar> bar)
{
var query = from x in bar
select new Foo()
{
foo = x.foo,
bar = x.bar
};
return query.ToDictionary(f=>f.foo,f=>f);
}