用哪个c#集合来代替List>
本文关键字:KeyValuePair string double List 集合 | 更新日期: 2023-09-27 18:09:10
我想存储如下数据
{
{"apple",15 }
{"pear",12.5 }
{"", 10 }
{"", 0.45 }
}
数据将绘制在条形图上(字符串将是图例,double将是值)插入顺序很重要。不重要。字符串可以重复,也可以为空。(值也可以重复)
我需要得到最小值和最大值(如果可能的话很容易)来设置比例。
我使用List<KeyValuePair<string, double>> data = new List<KeyValuePair<string, double>>();
data.Add(new KeyValuePair<string,double>("",i));
相当无聊和不可读。有没有更干净的方法呢?
StringDoubleCollection data = new StringDoubleCollection();
data.add("apple",15);
data.add("",10);
double max = data.values.Max();
double min = data.values.Min();
如果没有,如何在没有太多麻烦的情况下获得List<KeyValuePair<string, double>>
的最大值
NameValueCollection看起来不错,但它是<string,string>
,我需要<string,double>
您可以像这样创建一个类:
class X
{
public string Name { get; set; }
public double Value { get; set; }
// name is an optional parameter (this means it can be used only in C# 4)
public X(double value, string name = "")
{
this.Name = name;
this.Value = value;
}
// whatever
}
然后使用LINQ与选择器获得最大值和最小值:
var data = new List<X>();
data.Add(new X(35.0, "Apple"))
data.Add(new X(50.0));
double max = data.Max(a => a.Value);
double min = data.Min(a => a.Value);
EDIT:如果上面的代码对你来说仍然是不可读的,尝试使用一个操作符来改进它,在你只想要值的情况下。
// Inside X class...
public static implicit operator X(double d)
{
return new X(d);
}
// Somewhere else...
data.Add(50.0);
要确定您真正想要的数据结构,让我们看看您的使用模式。
- 你不能通过键访问你的物品。
- 你想要最小值和最大值
堆提供最小或最大值,但不保持顺序。基于散列的字典也不保持顺序。对于您的数据结构来说,List实际上是一个不错的选择。它是可用的,并提供了出色的支持。
您可以通过为数据结构和bar数据定义类来美化代码。您还可以在集合中添加最小/最大功能。注意:我没有使用Linq最小/最大函数,因为它们返回最小值,而不是最小元素。
public class BarGraphData {
public string Legend { get; set; }
public double Value { get; set; }
}
public class BarGraphDataCollection : List<BarGraphData> {
// add necessary constructors, if any
public BarGraphData Min() {
BarGraphData min = null;
// finds the minmum item
// prefers the item with the lowest index
foreach (BarGraphData item in this) {
if ( min == null )
min = item;
else if ( item.Value < min.Value )
min = item;
}
if ( min == null )
throw new InvalidOperationException("The list is empty.");
return min;
}
public BarGraphData Max() {
// similar implementation as Min
}
}
你看过LookUp吗?
唯一的问题是它是不可变的,所以你需要能够一次创建你的集合。
正如Anthony Pegram所指出的,创建一个是有点痛苦的。这取决于你的数据来自哪里。看一下tollookup方法
如果这是值得的可用性(即您使用List<KeyValuePair<string, double>>
的尴尬集合无处不在,它可能只是值得实现StringDoubleCollection
。用您在示例中描述的更友好的语法包装底层集合并不是那么困难。
而且,正如其他评论/答案所暗示的那样,框架似乎并没有提供一个更简单的解决方案来满足您的所有需求…
关于"最大值",我假设您指的是具有最大值的键值对。它可以像这样检索:
var max = list.Select(kvp => kvp.Value).Max();
只需定义自己的模型类来保存数据,而不是依赖于KeyValuePair,一切都变得更清晰:
using System;
using System.Collections.Generic;
public class Fruit
{
public string Name {get; set;}
public double Price {get; set;}
}
public class Program
{
public static void Main()
{
List<Fruit> _myFruit = new List<Fruit>();
_myFruit.Add(new Fruit{Name="apple", Price=15 });
_myFruit.Add(new Fruit{Name="pear", Price=12.5 });
_myFruit.Add(new Fruit{Name="", Price=10 });
_myFruit.Add(new Fruit{Name="", Price=0.45 });
// etc...
}
}
如何实现StringDoubleCollection工作像你想要的…
public class StringDoubleCollection
{
private List<KeyValuePair<string, double>> myValues;
public List<double> values
{
get { return myValues.Select(keyValuePair => keyValuePair.Value).ToList(); }
}
public void add(string key, double value)
{
myValues.Add(new KeyValuePair<string,double>(key,value));
}
}
可以实现Dictionary<key,>
Dictionary<string, string> openWith = new Dictionary<string, string>();
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
https://learn.microsoft.com/pt br/dotnet/api/system.collections.generic.dictionary - 2 ?view=net - 5.0