C#字典一键很多值

我想创建一个数据存储以允许我存储一些数据。

第一个想法是创建一个字典,在该字典中您拥有一个带有多个值的键,因此有点像一对多的关系。

我认为字典只有1个键值。

我还能如何存储此信息?


慕村225694
浏览 1392回答 3
3回答

撒科打诨

从.net3.5 +开始,Dictionary<IKey, List<IValue>>可以使用LookupLinq命名空间中的a 来代替使用a :// lookup Order by payment status (1:m)&nbsp;// would need something like Dictionary<Boolean, IEnumerable<Order>> orderIdByIsPayedILookup<Boolean, Order> byPayment = orderList.ToLookup(o => o.IsPayed);IEnumerable<Order> payedOrders = byPayment[false];来自msdn:查找类似于字典。区别在于字典将键映射到单个值,而查阅将键映射到值的集合。您可以通过在实现IEnumerable的对象上调用ToLookup来创建Lookup的实例。您可能还需要阅读这个答案的相关问题。有关更多信息,请咨询msdn。完整示例:using System;using System.Collections.Generic;using System.Linq;namespace LinqLookupSpike{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(String[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // init&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var orderList = new List<Order>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(1, 1, 2010, true));//(orderId, customerId, year, isPayed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(2, 2, 2010, true));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(3, 1, 2010, true));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(4, 2, 2011, true));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(5, 2, 2011, false));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(6, 1, 2011, true));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderList.Add(new Order(7, 3, 2012, false));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // lookup Order by its id (1:1, so usual dictionary is ok)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary<Int32, Order> orders = orderList.ToDictionary(o => o.OrderId, o => o);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // lookup Order by customer (1:n)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // would need something like Dictionary<Int32, IEnumerable<Order>> orderIdByCustomer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ILookup<Int32, Order> byCustomerId = orderList.ToLookup(o => o.CustomerId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var customerOrders in byCustomerId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Customer {0} ordered:", customerOrders.Key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var order in customerOrders)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("&nbsp; &nbsp; Order {0} is payed: {1}", order.OrderId, order.IsPayed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the same using old fashioned Dictionary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary<Int32, List<Order>> orderIdByCustomer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderIdByCustomer = byCustomerId.ToDictionary(g => g.Key, g => g.ToList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var customerOrders in orderIdByCustomer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Customer {0} ordered:", customerOrders.Key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var order in customerOrders.Value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("&nbsp; &nbsp; Order {0} is payed: {1}", order.OrderId, order.IsPayed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // lookup Order by payment status (1:m)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // would need something like Dictionary<Boolean, IEnumerable<Order>> orderIdByIsPayed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ILookup<Boolean, Order> byPayment = orderList.ToLookup(o => o.IsPayed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<Order> payedOrders = byPayment[false];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var payedOrder in payedOrders)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Order {0} from Customer {1} is not payed.", payedOrder.OrderId, payedOrder.CustomerId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; class Order&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // key properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Int32 OrderId { get; private set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Int32 CustomerId { get; private set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Int32 Year { get; private set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Boolean IsPayed { get; private set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // additional properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // private List<OrderItem> _items;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Order(Int32 orderId, Int32 customerId, Int32 year, Boolean isPayed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OrderId = orderId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CustomerId = customerId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Year = year;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsPayed = isPayed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}关于不变性的评论默认情况下,查找是不可变的,访问internals将涉及反射。如果您需要可变性并且不想编写自己的包装器,则可以使用corefxlab中的(以前MultiValueDictionary称为MultiDictionary)(以前不再更新其中的一部分)。Microsoft.Experimental.Collections

开心每一天1111

字典的值类型可以是List或包含多个对象的其他类。就像是Dictionary<int, List<string>>&nbsp;用于由int键控并包含字符串列表的Dictionary。选择值类型的主要考虑因素是将使用Dictionary的目的,如果您必须对值进行搜索或其他操作,那么也许考虑考虑使用一种可以帮助您完成所需工作的数据结构- -像HashSet。
打开App,查看更多内容
随时随地看视频慕课网APP