gpt4 book ai didi

c# - SortedList 可以为每个值取两个键吗?

转载 作者:行者123 更新时间:2023-11-30 17:02:38 26 4
gpt4 key购买 nike

可以SortedList取两个键并返回一个值?

我有 SortedList<string, double> 类型的标准

但现在我需要传递两个键来找到值 SortedList<string, string, double> .

这可能吗?如果没有,请告诉我其他一些解决方案,我构建了一个SortedList使用 string 和 double,但现在我发现我需要基于两个字符串“调用”那个 double。

List<string> StatsCheckBoxList = new List<string>();
List<string> PeriodCheckBoxList = new List<string>();


if (checkBox7.Checked)
PeriodCheckBoxList.Add(checkBox7.Text);
if (checkBox8.Checked)
PeriodCheckBoxList.Add(checkBox8.Text);
if (checkBox9.Checked)
PeriodCheckBoxList.Add(checkBox9.Text);
if (checkBox10.Checked)
PeriodCheckBoxList.Add(checkBox10.Text);

if (checkBox19.Checked)
StatsCheckBoxList.Add(checkBox19.Text);
if (checkBox35.Checked)
StatsCheckBoxList.Add(checkBox35.Text);
if (checkBox34.Checked)
StatsCheckBoxList.Add(checkBox34.Text);


// print the name of stats onto the first column:
int l = 0;
foreach (string Stats in StatsCheckBoxList)
{

NewExcelWorkSheet.Cells[ProductReturnRawData.Count + PeriodCheckBoxList.Count + 20 + l, 1] = Stats;

l++;
}

// print the time period of each stats onto the row above:
int h = 0;
foreach (string period in PeriodCheckBoxList)
{

NewExcelWorkSheet.Cells[ProductReturnRawData.Count + PeriodCheckBoxList.Count + 19, 2 + h] = period;

h++;
}

//这是一个数据表,现在我已经根据用户选择将统计名称打印到第一列,我还根据用户选择将时间段打印到顶行。现在我需要根据选择的统计数据和期间来调用统计数据的值。所以我需要将两个键传递给我的 SortedList。像这样的东西:

NewExcelWorkSheet.Cells[ProductReturnRawData.Count + 27, 2] = Convert.ToString(productReturnValue["3 Months"]); 

这里的 productReturnValue 是一个 SortedList,它接受一个字符串键并返回一个 double 值。

最佳答案

你需要一个 Tuple元数 2:

public SortedList<Tuple<string,string>,double> mySortedList ;

尽管您可能必须为其提供自定义比较器,例如:

class My2TupleComparer : IComparer<Tuple<string,string>
{
public int Compare(Tuple<string,string> x, Tuple<string,string> y )
{
int cc ;
if ( x == null && y == null ) cc = 0 ;
else if ( x == null && y != null ) cc = -1 ;
else if ( x != null && y == null ) cc = +1 ;
else /* ( x != null && y != null ) */
{
cc = string.Compare(x.Item1 , y.Item1 , StringComparison.OrdinalIgnoreCase ) ;
if ( cc == 0 )
{
cc = String.Compare( x.Item2 , y.Item2 , StringComparison.OrdinalIgnoreCase ) ;
}
}
return cc ;
}
}

关于c# - SortedList 可以为每个值取两个键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19552159/

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