开发者

Which was the IGrouping collection in .net?

I need to use IGrouping objects which implement it but i don't remember which collection was iGrouping.

Example which i want to do :

var grouper = new Grouper<string,string>();
grouper.Add("car","ford");
grouper.Add("c开发者_开发技巧ar","mercedes");
string[] cars = grouper["car"]; // cars = {"ford","mercedes"};

Grouper is dummy class in here


Enumerable.ToLookup() returns a collection of IGrouping's (an ILookup) - if that's what you were looking for.

MSDN sample:

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
    public long TrackingNumber { get; set; }
}

public static void ToLookupEx1()
{
    // Create a list of Packages.
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", 
                    Weight = 25.2, TrackingNumber = 89453312L },
                new Package { Company = "Lucerne Publishing", 
                    Weight = 18.7, TrackingNumber = 89112755L },
                new Package { Company = "Wingtip Toys", 
                    Weight = 6.0, TrackingNumber = 299456122L },
                new Package { Company = "Contoso Pharmaceuticals", 
                    Weight = 9.3, TrackingNumber = 670053128L },
                new Package { Company = "Wide World Importers", 
                    Weight = 33.8, TrackingNumber = 4665518773L } };

    // Create a Lookup to organize the packages. 
    // Use the first character of Company as the key value.
    // Select Company appended to TrackingNumber 
    // as the element values of the Lookup.
    ILookup<char, string> lookup =
        packages
        .ToLookup(p => Convert.ToChar(p.Company.Substring(0, 1)),
                    p => p.Company + " " + p.TrackingNumber);

    // Iterate through each IGrouping in the Lookup.
    foreach (IGrouping<char, string> packageGroup in lookup)
    {
        // Print the key value of the IGrouping.
        Console.WriteLine(packageGroup.Key);
        // Iterate through each value in the 
        // IGrouping and print its value.
        foreach (string str in packageGroup)
            Console.WriteLine("    {0}", str);
    }
}


It seems that you need an ILookup<TK,TV> implementation, but unfortunately the one used by ToLookup() LINQ method is not public.

Anyway that's easy to implement (in particular if someone like Jon Skeet already did)

public sealed class Lookup<TKey, TElement> : ILookup<TKey, TElement>
{
    private readonly Dictionary<TKey, List<TElement>> map;
    private readonly List<TKey> keys;

    public Lookup()
        : this(EqualityComparer<TKey>.Default)
    { }

    public Lookup(IEqualityComparer<TKey> comparer)
    {
        map = new Dictionary<TKey, List<TElement>>(comparer);
        keys = new List<TKey>();
    }

    public void Add(TKey key, TElement element)
    {
        List<TElement> list;
        if (!map.TryGetValue(key, out list))
        {
            list = new List<TElement>();
            map[key] = list;
            keys.Add(key);
        }
        list.Add(element);
    }

    public int Count
    {
        get { return map.Count; }
    }

    public IEnumerable<TElement> this[TKey key]
    {
        get
        {
            List<TElement> list;
            if (!map.TryGetValue(key, out list))
            {
                return Enumerable.Empty<TElement>();
            }
            return list.Select(x => x);
        }
    }

    public bool Contains(TKey key)
    {
        return map.ContainsKey(key);
    }

    public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
    {
        return keys.Select(key => new Grouping<TKey, TElement>(key, map[key]))
                    .GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public sealed class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private readonly TKey key;
    private readonly IEnumerable<TElement> elements;

    public Grouping(TKey key, IEnumerable<TElement> elements)
    {
        this.key = key;
        this.elements = elements;
    }

    public TKey Key { get { return key; } }

    public IEnumerator<TElement> GetEnumerator()
    {
        return elements.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Usage:

var lookup = new Lookup<string, string>();
lookup.Add("car", "ford");
lookup.Add("car", "mercedes");
var cars = lookup["car"]; 
// cars is an IEnumerable<string> containing {"ford","mercedes"}

That is really similar to your request, isn't it ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜