2011-03-19 2 views
0

구현하는 IGrouping 개체를 사용해야하지만 iGrouping 컬렉션을 기억하지 않습니다..net의 IGrouping 컬렉션은 무엇입니까?

예하는 내가하고 싶은 : 여기

+1

사전 >와 같은 사전을 사용하여 키별로 개체를 그룹화 할 수 있지만 그룹화 사전은 이보다 더 간단합니다. – Freshblood

답변

3

Enumerable.ToLookup()에서

var grouper = new Grouper<string,string>(); 
grouper.Add("car","ford"); 
grouper.Add("car","mercedes"); 
string[] cars = grouper["car"]; // cars = {"ford","mercedes"}; 

그룹화 더미 클래스는 IGrouping 년대 (AN ILookup)의 컬렉션을 반환 - 그것이 당신이 찾고 있던 거라면.

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); 
    } 
} 
1

당신이 ILookup<TK,TV> 구현이 필요하다는 것,하지만 불행히도 ToLookup() LINQ 방식에서 사용하는 공개되지 않습니다. 귀하의 요청에 정말 비슷합니다

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"} 

, 그렇지 않은 :

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(); 
    } 
} 

사용이 쉬운

어쨌든은 (사람이 같은 Jon Skeet already did이 경우 특히) 구현 ?

관련 문제