看來你需要一個ILookup<TK,TV>
實現,但不幸被ToLookup()
LINQ方法中使用的是不公開的。
反正是很容易實現(特別是如果有人像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();
}
}
用法:
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"}
這真的是類似於你的要求,是不是?
可以通過密鑰將對象分組,如本字典<字符串列表>但分組字典將比此更簡單。 –
Freshblood
2011-03-19 14:19:40