serhio
serhio

Reputation: 28586

Homework: HowTo obtain multiple keys collection?

I have

public class Letter
{
    public string Value;
    public int Id;

    public Letter(string val, int id)
    {
        this.Value = val;
        this.Id = id;
    }
}

I need a kind of duplicate dictionary (LookUp(?)) for:

private something TestCollection()
{
    List<Letter> inputList = new List<Letter> { 
        new Letter("a", 9), 
        new Letter("b", 5), 
        new Letter("c", 8), 
        new Letter("aIdentic", 9) 
    };

    // compare inputList by letter's ID(!)
    // use inputList (zero based) INDEXES as values

    // return something, like LookUp: { "a"=>(0, 3), "b"=>(1), "c"=>(2) };

}

using .NET 4

How to obtain it?

As I understand, there is 2 solutions, one from .NET 4, Lookup<Letter, int>, other, classic one Dictionary<Letter, List<int>>

thanks.

EDIT:

For output. There is 2 letters "a", identified by ID 9 on index "0" in the array(first position). "b" have index 1 (second position in the input array), "c" - index 2 (is third).

EDIT 2

John solution:

    public class Letter
    {
        public string Value;
        public int Id;

        public Letter(string val, int id)
        {
            this.Value = val;
            this.Id = id;
        }
    }

    private void btnCommand_Click(object sender, EventArgs e)
    {
        List<Letter> inputList = new List<Letter> { 
            new Letter("a", 9), 
            new Letter("b", 5), 
            new Letter("c", 8), 
            new Letter("aIdentic", 9) 
        };

        var lookup = inputList.Select((value, index) =>
            new { value, index }).ToLookup(x => x.value, x => x.index);

        // outputSomething { "a"=>(0, 3), "b"=>(1), "c"=>(2) };
        foreach (var item in lookup)
        {
            Console.WriteLine("{0}: {1}", item.Key, item.ToString());
        }

    }

Output (I expect no more than 3 keys):

//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]

EDIT 3 Equals

public override bool Equals(object obj)
{
    if (obj is Letter)
        return this.Id.Equals((obj as Letter).Id);
    else
        return base.Equals(obj);
}

public override int  GetHashCode()
{
    return this.Id;
}

Upvotes: 0

Views: 480

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503270

Lookup is probably the right class to use here - and LINQ lets you build one with ToLookup. Note that Lookup was introduced in .NET 3.5, not .NET 4.

Having said that, it's not at all clear how you'd go from your input to your sample output...

EDIT: Okay, now that I understand you're after the index, you might want to use the overload of Select which includes an index first:

var lookup = inputList.Select((value, index) => new { value, index })
                      .ToLookup(x => x.value.Id, x => x.index);

Upvotes: 1

Related Questions