C# 字典——一键多值 [英] C# dictionary - one key, many values
问题描述
我想创建一个数据存储来存储一些数据.
I want to create a data store to allow me to store some data.
第一个想法是创建一个字典,其中有一个键和多个值,有点像一对多关系.
The first idea was to create a dictionary where you have one key with many values, so a bit like a one-to-many relationship.
我认为字典只有一个键值.
I think the dictionary only has one key value.
我还能如何存储这些信息?
How else could I store this information?
推荐答案
从 .NET 3.5+ 开始,您可以使用 Dictionary
,而不是使用Lookup
从 LINQ 命名空间:
As of .NET 3.5+, instead of using a Dictionary<IKey, List<IValue>>
, you can use a Lookup
from the LINQ namespace:
// Lookup Order by payment status (1:m)
// would need something like Dictionary<Boolean, IEnumerable<Order>> orderIdByIsPayed
ILookup<Boolean, Order> byPayment = orderList.ToLookup(o => o.IsPayed);
IEnumerable<Order> payedOrders = byPayment[false];
来自 MSDN:
查找
A Lookup<TKey, TElement> resembles a Dictionary<TKey, TValue>. The difference is that a Dictionary<TKey, TValue> maps keys to single values, whereas a Lookup<TKey, TElement> maps keys to collections of values.
您可以创建一个 Lookup
You can create an instance of a Lookup<TKey, TElement> by calling ToLookup on an object that implements IEnumerable.
您可能还想阅读这个答案到相关问题.有关详细信息,请参阅 MSDN.
You may also want to read this answer to a related question. For more information, consult MSDN.
完整示例:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqLookupSpike
{
class Program
{
static void Main(String[] args)
{
// Init
var orderList = new List<Order>();
orderList.Add(new Order(1, 1, 2010, true)); // (orderId, customerId, year, isPayed)
orderList.Add(new Order(2, 2, 2010, true));
orderList.Add(new Order(3, 1, 2010, true));
orderList.Add(new Order(4, 2, 2011, true));
orderList.Add(new Order(5, 2, 2011, false));
orderList.Add(new Order(6, 1, 2011, true));
orderList.Add(new Order(7, 3, 2012, false));
// Lookup Order by its id (1:1, so usual dictionary is ok)
Dictionary<Int32, Order> orders = orderList.ToDictionary(o => o.OrderId, o => o);
// Lookup Order by customer (1:n)
// would need something like Dictionary<Int32, IEnumerable<Order>> orderIdByCustomer
ILookup<Int32, Order> byCustomerId = orderList.ToLookup(o => o.CustomerId);
foreach (var customerOrders in byCustomerId)
{
Console.WriteLine("Customer {0} ordered:", customerOrders.Key);
foreach (var order in customerOrders)
{
Console.WriteLine(" Order {0} is payed: {1}", order.OrderId, order.IsPayed);
}
}
// The same using old fashioned Dictionary
Dictionary<Int32, List<Order>> orderIdByCustomer;
orderIdByCustomer = byCustomerId.ToDictionary(g => g.Key, g => g.ToList());
foreach (var customerOrders in orderIdByCustomer)
{
Console.WriteLine("Customer {0} ordered:", customerOrders.Key);
foreach (var order in customerOrders.Value)
{
Console.WriteLine(" Order {0} is payed: {1}", order.OrderId, order.IsPayed);
}
}
// Lookup Order by payment status (1:m)
// would need something like Dictionary<Boolean, IEnumerable<Order>> orderIdByIsPayed
ILookup<Boolean, Order> byPayment = orderList.ToLookup(o => o.IsPayed);
IEnumerable<Order> payedOrders = byPayment[false];
foreach (var payedOrder in payedOrders)
{
Console.WriteLine("Order {0} from Customer {1} is not payed.", payedOrder.OrderId, payedOrder.CustomerId);
}
}
class Order
{
// Key properties
public Int32 OrderId { get; private set; }
public Int32 CustomerId { get; private set; }
public Int32 Year { get; private set; }
public Boolean IsPayed { get; private set; }
// Additional properties
// private List<OrderItem> _items;
public Order(Int32 orderId, Int32 customerId, Int32 year, Boolean isPayed)
{
OrderId = orderId;
CustomerId = customerId;
Year = year;
IsPayed = isPayed;
}
}
}
}
关于不变性的评论
默认情况下,lookups 是不可变的,访问 internal
将涉及反射.如果您需要可变性并且不想编写自己的包装器,则可以使用 MultiValueDictionary
(以前称为 MultiDictionary
) 来自 corefxlab(以前属于Microsoft.Experimental.Collections
不再更新).
By default, lookups are kind of immutable and accessing the internal
s would involve reflection.
If you need mutability and don't want to write your own wrapper, you could use MultiValueDictionary
(formerly known as MultiDictionary
) from corefxlab (formerly part ofMicrosoft.Experimental.Collections
which isn't updated anymore).
这篇关于C# 字典——一键多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!