gpt4 book ai didi

C# 字典 - 一个键,多个值

转载 作者:IT王子 更新时间:2023-10-29 03:44:17 27 4
gpt4 key购买 nike

我想创建一个数据存储来存储一些数据。

第一个想法是创建一个字典,其中有一个键和多个值,有点像一对多关系。

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

我还能如何存储这些信息?

最佳答案

从 .NET 3.5+ 开始,不再使用 Dictionary<IKey, List<IValue>> , 你可以使用 Lookup来自 LINQ 命名空间:

// 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>. Thedifference is that a Dictionary<TKey, TValue> maps keys to singlevalues, whereas a Lookup<TKey, TElement> maps keys to collections ofvalues.

You can create an instance of a Lookup<TKey, TElement> by callingToLookup on an object that implements IEnumerable.

您可能还想阅读 this answerrelated question .更多信息,请咨询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;
}
}
}
}

关于不变性的评论

默认情况下,查找 是不可变的并且访问 internal s 将涉及反射(reflection)。如果您需要可变性并且不想编写自己的包装器,则可以使用 MultiValueDictionary (以前称为 MultiDictionary )来自 corefxlab (以前是 Microsoft.Experimental.Collections 的一部分,现在不再更新)。

关于C# 字典 - 一个键,多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2101069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com