ठीक है, अपने उदाहरण में आप प्रभावी रूप से बस के साथ Id
= 1 Customer
वस्तु के लिए प्रवेश पता लगा रहे हैं और संबद्ध मूल्य को अद्यतन करने। प्रैक्टिस में, मुझे लगता है कि आपका कोड शब्दकोश में संबंधित मान को अपडेट करने से पहले आपके इच्छित Customer
ऑब्जेक्ट का संदर्भ प्राप्त करने में सक्षम होगा। यदि ऐसा है, तो लूप की कोई आवश्यकता नहीं है।
नीचे एक बहुत ही सरल उदाहरण है जहां एक लूप की आवश्यकता नहीं है क्योंकि आपके कोड में पहले से ही customer1
चर का संदर्भ है। जबकि मेरा उदाहरण अत्यधिक सरलीकृत है, अवधारणा यह है कि आप संभवतः अपने वांछित Customer
ऑब्जेक्ट को संदर्भित करने के अलावा कुछ अन्य माध्यमों के माध्यम से संदर्भ प्राप्त कर सकते हैं।
static void Main(string[] args)
{
Dictionary<Customer, int> CustomerOrderDictionary = new Dictionary<Customer, int>();
Customer customer1 = new Customer { Id = 1, FullName = "Jo Bloogs" };
Customer customer2 = new Customer { Id = 2, FullName = "Rob Smith" };
CustomerOrderDictionary.Add(customer1, 3);
CustomerOrderDictionary.Add(customer2, 5);
// you already have a reference to customer1, so just use the accessor on the dictionary to update the value
CustomerOrderDictionary[customer1]++;
}
आप कुछ अन्य मानदंडों के आधार पर Customer
वस्तुओं एकाधिक पर अद्यतन किसी तरह का प्रदर्शन करने के लिए की जरूरत है, तो आप एक पाश आवश्यकता हो सकती है। निम्नलिखित उदाहरण मानते हैं कि आपके पास Customer
ऑब्जेक्ट्स को संग्रहीत करने वाले शब्दकोश के अलावा कुछ संग्रह होगा, और आप Customer
ऑब्जेक्ट्स के उस संग्रह का उपयोग कर सकते हैं, जिनकी पहचान करने के लिए शब्दकोश में संबंधित मूल्य को अपडेट किया जाना चाहिए।
static void Main(string[] args)
{
// presumably you will have a separate collection of all your Customer objects somewhere
List<Customer> customers = new List<Customer>();
Customer customer1 = new Customer { Id = 1, FullName = "Jo Bloogs" };
Customer customer2 = new Customer { Id = 2, FullName = "Rob Smith" };
Customer customer3 = new Customer { Id = 3, FullName = "Rob Zombie" };
customers.Add(customer1);
customers.Add(customer2);
customers.Add(customer3);
Dictionary<Customer, int> CustomerOrderDictionary = new Dictionary<Customer, int>();
CustomerOrderDictionary.Add(customer1, 3);
CustomerOrderDictionary.Add(customer2, 5);
// let's just say that we're going to update the value for any customers whose name starts with "Rob"
// use the separate list of Customer objects for the iteration,
// because you would not be allowed to modify the dictionary if you iterate over the dictionary directly
foreach (var customer in customers.Where(c => c.FullName.StartsWith("Rob")))
{
// the dictionary may or may not contain an entry for every Customer in the list, so use TryGetValue
int value;
if (CustomerOrderDictionary.TryGetValue(customer, out value))
// if an entry is found for this customer, then increment the value of that entry by 1
CustomerOrderDictionary[customer] = value + 1;
else
// if there is no entry in the dictionary for this Customer, let's add one just for the heck of it
CustomerOrderDictionary.Add(customer, 1);
}
}
यदि यह स्थिति और Customer
वस्तुओं है कि आप उपलब्ध है का एकमात्र स्रोत नहीं है शब्दकोश में ही है, तो आप एक अलग सूची के लिए बाहर उन वस्तुओं की क्लोनिंग/नकल किसी तरह का प्रदर्शन करने के लिए की आवश्यकता होगी संशोधन के लिए शब्दकोश पर पुनरावृत्त करने से पहले/सरणी। इस मामले के लिए जॉन स्कीट का जवाब देखें; वह फ़िल्टर का उपयोग Keys
संपत्ति पर फ़िल्टर करने का सुझाव देता है और पुनरावृत्ति के उद्देश्य के लिए अलग List<Customer>
उदाहरण बनाने के लिए ToList
विधि का उपयोग करता है।
हाय, धन्यवाद बहुत काम करता है। मैं यह देखने की कोशिश करूंगा कि यह वास्तविक कोड के साथ काम करता है। धन्यवाद – user9969