Tôi hiện đang có một menu với subitems đang được lưu trữ trong biến từ điển này:Có bộ sưu tập <string, object> từ điển nào cho phép nhiều khóa không?
private Dictionary<string, UserControl> _leftSubMenuItems
= new Dictionary<string, UserControl>();
Vì vậy, tôi thêm quan điểm cho ví dụ các "khách hàng" phần như thế này:
_leftSubMenuItems.Add("customers", container.Resolve<EditCustomer>());
_leftSubMenuItems.Add("customers", container.Resolve<CustomerReports>());
Nhưng kể từ khi tôi đang sử dụng một từ điển, tôi chỉ có thể có một khóa có tên "khách hàng".
xu hướng tự nhiên của tôi sẽ được đến nay tạo ra một cấu trúc tùy chỉnh với thuộc tính "Mục" và "Chế độ xem", nhưng là có một bộ sưu tập NET là phù hợp hơn cho nhiệm vụ này, một cái gì đó giống như một "MultiKeyDictionary"?
ĐÁP:
Cảm ơn maciejkow, tôi mở rộng đề xuất của bạn để có được chính xác những gì tôi cần:
using System;
using System.Collections.Generic;
namespace TestMultiValueDictionary
{
class Program
{
static void Main(string[] args)
{
MultiValueDictionary<string, object> leftSubMenuItems = new MultiValueDictionary<string, object>();
leftSubMenuItems.Add("customers", "customers-view1");
leftSubMenuItems.Add("customers", "customers-view2");
leftSubMenuItems.Add("customers", "customers-view3");
leftSubMenuItems.Add("employees", "employees-view1");
leftSubMenuItems.Add("employees", "employees-view2");
foreach (var leftSubMenuItem in leftSubMenuItems.GetValues("customers"))
{
Console.WriteLine(leftSubMenuItem);
}
Console.WriteLine("---");
foreach (var leftSubMenuItem in leftSubMenuItems.GetAllValues())
{
Console.WriteLine(leftSubMenuItem);
}
Console.ReadLine();
}
}
public class MultiValueDictionary<TKey, TValue> : Dictionary<TKey, List<TValue>>
{
public void Add(TKey key, TValue value)
{
if (!ContainsKey(key))
Add(key, new List<TValue>());
this[key].Add(value);
}
public List<TValue> GetValues(TKey key)
{
return this[key];
}
public List<TValue> GetAllValues()
{
List<TValue> list = new List<TValue>();
foreach (TKey key in this.Keys)
{
List<TValue> values = this.GetValues(key);
list.AddRange(values);
}
return list;
}
}
}
Trả lời 2:
Cảm ơn Blixt for the tip về năng suất, đây là GetAllValues với thay đổi đó:
public IEnumerable<TValue> GetAllValues()
{
foreach (TKey key in this.Keys)
{
List<TValue> values = this.GetValuesForKey(key);
foreach (var value in values)
{
yield return value;
}
}
}
Trả lời 2 lại yếu tố nữa:
Dưới đây là một cách rất gọn gàng hơn để làm điều tương tự, nhờ Keith:
public IEnumerable<TValue> GetAllValues()
{
foreach (var keyValPair in this)
foreach (var val in keyValPair.Value)
yield return val;
}
Đối với phương thức 'GetAllValues', bạn có thể sử dụng từ khóa' yield' thay vì tạo danh sách mới, sử dụng nhiều bộ nhớ hơn và kém hiệu quả hơn. – Blixt
Đó có thể là tất cả những gì bạn cần, nhưng nó hầu như không phải là một MultiDictionary hoàn chỉnh. Điều gì về 'Remove'? Liệt kê các giá trị? vv. –
Thực tế, giải pháp thích hợp ở đây là không phát minh lại bánh xe và sử dụng một trong các thư viện được liên kết trong các câu trả lời hoặc tìm kiếm một thư viện khác làm nhiều hơn những gì bạn cần. Đối với một mục đích chung như vậy, bạn chắc chắn sẽ tìm được một ứng dụng được kiểm tra và tối ưu hóa tốt. – Blixt