4

SortedList2 つのキーを取り、1 つの値を返すことはできますか?

私はタイプの標準を持っていますSortedList<string, double>

しかし今、値を見つけるために 2 つのキーを渡す必要がありますSortedList<string, string, double>

これは可能でしょうか?そうでない場合は、他の解決策を教えてください。SortedList文字列と double を使用して作成しましたが、2 つの文字列に基づいてその 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 に 2 つのキーを渡す必要があります。何かのようなもの:

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

ここで、productReturnValue は、文字列キーを受け取り、double 値を返す SortedList です。

4

1 に答える 1

2

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 ;
  }
}
于 2013-10-23T22:08:23.377 に答える