0

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);            
}
jellz77
  • 324
  • 4
  • 15
  • 2
    How are you expecting this to work? Which element of the sequence should `foo["bar"]` return? – Lee Jun 14 '16 at 18:30
  • i'm currently running a foreach within a foreach and saying `if item.foo==item2.foo then ....`. I was trying to bypass this logic by just running 1 foreach and accessing the other IEnumerable by the item.foo. that make sense? – jellz77 Jun 14 '16 at 18:31
  • Can you add the `foreach` you had to the question? – Lee Jun 14 '16 at 18:32
  • 2
    You should consider using a dictionary rather. – Rahul Jun 14 '16 at 18:33
  • @jellz77: `"that make sense?"` - No, not really. – David Jun 14 '16 at 18:35
  • @Rahul can i store multiple values in a dictionary or just Key->Value? I come from a PHP background so i'm really looking for a multi-dimensional array – jellz77 Jun 14 '16 at 18:35
  • sounds like you want a `Dictionary` instead. – juharr Jun 14 '16 at 18:35
  • @jellz77 you can store multiple Key-Value pairs, but the key has to be unique and not null. – juharr Jun 14 '16 at 18:36
  • @jellz77 How would `foo["bar"].foo` work if there were multiple objects mapped to `"bar"`? It would be a lot more clear if you wouldn't reuse the type names for variables and properties. – D Stanley Jun 14 '16 at 18:40
  • i think @juharr provided what i'm looking to do. – jellz77 Jun 14 '16 at 18:41

2 Answers2

1

There's nothing that compact with the List structure. It only has an indexer that accepts integers (the index of the item you want). A few options:

  • Subtype List<Foo> to add an indexer that accepts a string
  • Use a dictionary instead of a List<Foo>:

    public Dictionary<string,Foo> GetFoo(IEnumerable<Bar> bar)
    {
        var query = from x in bar       
                    select new Foo()
                    {
                        x.foo,
                        x.bar
                    };
    
        return query.ToDictionary(f => f.bar, f => f));
    }
    

then foo["bar"] will give the the Foo object indexed by "bar". You do not need to iterate the entire collection to find the object with the matching key.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • this did it! thanks! – jellz77 Jun 14 '16 at 18:42
  • the results are still being stored as ints, then the KeyValuePair? – jellz77 Jun 14 '16 at 18:53
  • in your example, i'd still have to iterate through the results in a foreach then access the key once in the foreach – jellz77 Jun 14 '16 at 18:54
  • I'm not sure what you man by either comment - a dictionary is designed for fast access by key - yes you still need to loop through if you want to iterate the keys. Your example indicated that you wanted _a specific key_, though. – D Stanley Jun 14 '16 at 18:57
  • correct so ideally i want to access foo["randomString1"].someValue without the implementation of a foreach. The reason i don't want to run a foreach is because i already have multiple foreach statements in my view and am looking for fast access to a KeyValue. – jellz77 Jun 14 '16 at 19:01
  • What I gave you gives you fast access by key value. I don't see where you're using `foreach` so I don't know what the problem is. But if you think you need to iterate the entire collection to find the matching key then see my update. – D Stanley Jun 14 '16 at 19:11
0

With a little more digging, i was able to find the answer through C# List of objects, how do I get the sum of a property

Using my example above, i implemented the following logic using linq:

METHOD

public List<Foo> GetFoo(IEnumerable<Bar> bar)
{
    List<Foo> foo = new List<Foo>();

    var query = from x in bar       
                select new Foo()
                {
                    name= x.name,
                    amount = x.amount
                };
    foo = query.ToList();
    return foo;
}

VIEW

@double total = foo.Where(item => item.name == "jon").Sum(item => item.amount);    
<td>@total<td>

within my view, i'd replace "jon" with a variable to alleviate a foreach within a foreach. sorry for the confusion all, but appreciate everyone's help!!

Community
  • 1
  • 1
jellz77
  • 324
  • 4
  • 15