2009-01-16 16 views
24

मेरे पास Dictionary<string, double> है और मैं इसे SortedDictionary<double, string> में परिवर्तित करना चाहता हूं। सी # 3.0 में LINQ एक्सटेंशन विधियों का उपयोग करके मैं इसे कैसे कर सकता हूं?LINQ में CQ का उपयोग करके मैं एक शब्दकोश से सॉर्टेड डिक्शनरी में कैसे परिवर्तित करूं?

संपादित करें: जेनेरिक कोण ब्रैकेट मूल प्रश्न में नहीं जब मार्क और जेरेड ने उत्तर दिया।

+1

आपका मतलब है SortedDictionary <स्ट्रिंग, डबल>? –

उत्तर

40

संपादित करें यह उत्तर संपादित करने से पहले था; अद्यतन समस्या के उत्तर के लिए, this reply देखें।

LINQ का उपयोग क्यों करें? - लेकिन मैं परेशान नहीं होता ...

+0

मुझे 20 सेकंड तक मारो – JaredPar

+0

बस एहसास हुआ कि मैंने अपने मूल प्रश्न में कोण ब्रैकेट को वापस उद्धृत नहीं किया है। – Guy

+0

मुझे खुशी है कि यह स्वीकार्य उत्तर है, इसके बावजूद कि ओपी ने क्या पूछने का इरादा नहीं दिया था। शीर्षक शीर्षक के लिए यह निश्चित रूप से सही जवाब है। –

8

कोई LINQ की जरूरत है

new SortedDictionary<int, string>(existing); 

आप एक ToSortedDictionary जोड़ सकते हैं: इस के लिए एक निर्माता है। सॉर्टेड डिक्शनरी में रूपांतरण करने के लिए एक कन्स्ट्रक्टर है।

public SortedDictionary<TKey,TValue> Convert<TKey,TValue>(Dictionary<TKey,TValue> map) { 
    return new SortedDictionary<TKey,TValue>(map); 
} 
1

आप LINQ, बस कुछ गंधा विस्तार के तरीकों की जरूरत नहीं है:

public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) 
{ 
    if(dictionary == null) 
    { 
     throw new ArgumentNullException("dictionary"); 
    } 

    return new SortedDictionary<TKey, TValue>(dictionary); 
} 

public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer) 
{ 
    if(dictionary == null) 
    { 
     throw new ArgumentNullException("dictionary"); 
    } 

    if(comparer == null) 
    { 
     throw new ArgumentNullException("comparer"); 
    } 

    return new SortedDictionary<TKey, TValue>(dictionary, comparer); 
} 

उदाहरण उपयोग:

var dictionary = new Dictionary<int, string> 
{ 
    { 1, "one" }, 
    { 2, "two" }, 
    { 0, "zero" } 
}; 

foreach(var pair in dictionary.Sort()) 
{ 
    Console.WriteLine("{0}: {1}", pair.Key, pair.Value); 
} 

// 0: zero 
// 1: one 
// 2: two 
5

ऐसा लगता है जैसे कि आप लेने के लिए एक सुंदर तरीका के लिए पूछ रहे हैं Dictionary<TKey,TValue> और इसे SortedDictionary<TValue,TKey> में बदलें (ध्यान दें कि Dictionary का मान अब SortedDictionary की कुंजी है)। मुझे कोई जवाब नहीं दिख रहा है, इसलिए यह यहां जाता है।

आप एक विस्तार विधि है कि इस तरह दिखता है बना सकते हैं:

static class Extensions 
{ 
    public static Dictionary<TValue, TKey> 
     AsInverted<TKey, TValue>(this Dictionary<TKey, TValue> source) 
    { 
     var inverted = new Dictionary<TValue, TKey>(); 

     foreach (KeyValuePair<TKey, TValue> key in source) 
      inverted.Add(key.Value, key.Key); 

     return inverted; 
    } 
} 

और आपके आवेदन कोड इस तरह दिखेगा:

using System; 
using System.Linq; 
using System.Collections.Generic; 

class Program 
{ 
    static void Main() 
    { 
     var dict = new Dictionary<String, Double>(); 
     dict.Add("four", 4); 
     dict.Add("three", 3); 
     dict.Add("two", 2); 
     dict.Add("five", 5); 
     dict.Add("one", 1); 

     var sortedDict = new SortedDictionary<Double, String>(dict.AsInverted()); 
    } 
} 
0

उलट ToDictionary का उपयोग कर:

public static IDictionary<TValue, TKey> Invert<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) 
{ 
    if(dictionary == null) 
    { 
     throw new ArgumentNullException("dictionary"); 
    } 

    return dictionary.ToDictionary(pair => pair.Value, pair => pair.Key); 
} 

उदाहरण उपयोग:

var dictionary = new Dictionary<string, int> 
{ 
    { "zero", 0 }, 
    { "one", 1 }, 
    { "two", 2 } 
}; 

foreach(var pair in dictionary.Invert()) 
{ 
    Console.WriteLine("{0}: {1}", pair.Key, pair.Value); 
} 

// 0: zero 
// 1: one 
// 2: two 

inverting का उदाहरण और (Sort की परिभाषा के लिए मेरे अन्य उत्तर देखें) छँटाई:

var dictionary = new Dictionary<string, int> 
{ 
    { "one", 1 }, 
    { "two", 2 }, 
    { "zero", 0 } 
}; 

foreach(var pair in dictionary.Invert().Sort()) 
{ 
    Console.WriteLine("{0}: {1}", pair.Key, pair.Value); 
} 

// 0: zero 
// 1: one 
// 2: two 
संबंधित मुद्दे