C# 如何根据字典数组中某个项的值获取该项的键?

C# 如何根据字典数组中某个项的值获取该项的键?,c#,asp.net,C#,Asp.net,以下方法将打印此文件: Minimum Temperature is 16 Maximum Temperature is 27 The Average Temperature is 22 现在,除了温度,我还想知道温度最高和最低的日子,比如: Minimum Temperature is 16 on Day 6 Maximum Temperature is 27 on Day 8 The Average Temperature is 22 这是一种将日和温度作为字典参数插入数组并将其传递给

以下方法将打印此文件:

Minimum Temperature is 16
Maximum Temperature is 27
The Average Temperature is 22 
现在,除了温度,我还想知道温度最高和最低的日子,比如:

Minimum Temperature is 16 on Day 6
Maximum Temperature is 27 on Day 8
The Average Temperature is 22
这是一种将日和温度作为字典参数插入数组并将其传递给确定最小值、最大值和平均值的方法

最小值和最大值是字典的int值,我的问题是如何根据这些值确定相关的字符串日期

 // if user select January , the January() execute:

 protected void Button1_Click(object sender, EventArgs e)
{
    // assigning Days and Temperatures to Dictionary and making array dictionary

    Dictionary<string, int>[] temperatures = new Dictionary<string, int>[10];
    temperatures[0] = new Dictionary<string, int>();
    temperatures[1] = new Dictionary<string, int>();
    temperatures[2] = new Dictionary<string, int>();
    temperatures[3] = new Dictionary<string, int>();
    temperatures[4] = new Dictionary<string, int>();
    temperatures[5] = new Dictionary<string, int>();
    temperatures[6] = new Dictionary<string, int>();
    temperatures[7] = new Dictionary<string, int>();
    temperatures[8] = new Dictionary<string, int>();
    temperatures[9] = new Dictionary<string, int>();

    temperatures[0].Add("Day1", 22);
    temperatures[1].Add("Day2", 23);
    temperatures[2].Add("Day3", 25);
    temperatures[3].Add("Day4", 26);
    temperatures[4].Add("Day5", 18);
    temperatures[5].Add("Day6", 16);
    temperatures[6].Add("Day7", 17);
    temperatures[7].Add("Day8", 27);
    temperatures[8].Add("Day9", 23);
    temperatures[9].Add("Day10", 24);
    if (DropDownList1.SelectedValue.ToString() == "January")
    {
        January(temperatures);
    }

  //the metthod which calculate min ,max and ..
 private void January(Dictionary<string, int>[] temperatures)
{
    int Minimumtemperture = 40;
    int Maximumtemperture = 0;
    int total = 0;
    int averageTemperatures = 0;
    // this foreach goes through array
    foreach (var temperture in temperatures)
    {

        // this foreach goes throuh dictionary 
        foreach (var degree in temperture)
        {                
            //assigning value of each dictionary to the monthTemp
            int MonthTemps = degree.Value;
            if (MonthTemps < Minimumtemperture)
            {
                Minimumtemperture = MonthTemps;
            }
            if (MonthTemps>Maximumtemperture)
            {
                Maximumtemperture = MonthTemps;
            }
            total = total + MonthTemps;

        }

        int totaltemperature = temperatures.Length;
        averageTemperatures = (total / totaltemperature);

    }

    // printing the result 

    Label1.Text = string.Format("Minimum Temperature is {0}<br/> Maximum Temperature is{1}<br/> The Average Temperature is{2}<br/>", Minimumtemperture, Maximumtemperture, averageTemperatures);

}
//如果用户选择一月,一月()将执行:
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
//为字典分配天数和温度并制作数组字典
字典[]温度=新字典[10];
温度[0]=新字典();
温度[1]=新字典();
温度[2]=新字典();
温度[3]=新字典();
温度[4]=新字典();
温度[5]=新字典();
温度[6]=新字典();
温度[7]=新字典();
温度[8]=新字典();
温度[9]=新字典();
温度[0]。添加(“第1天”,22);
温度[1]。添加(“第2天”,23);
温度[2]。添加(“第3天”,25);
温度[3]。添加(“第4天”,第26天);
温度[4]。添加(“第5天”,第18天);
温度[5]。添加(“第6天”,第16天);
温度[6]。添加(“第7天”,第17天);
温度[7]。添加(“第8天”,第27天);
温度[8]。添加(“第9天”,第23天);
温度[9]。添加(“第10天”,第24天);
if(DropDownList1.SelectedValue.ToString()=“一月”)
{
一月(气温);;
}
//计算最小值、最大值和..的方法。。
一月份私人空间(温度)
{
int最低温度=40;
int最大温度=0;
int-total=0;
int平均温度=0;
//这个foreach经过数组
foreach(温度中的var温度)
{
//这本词典通俗易懂
foreach(温度变化度)
{                
//将每个字典的值赋给monthTemp
int MonthTemps=度值;
if(月温度<最低温度)
{
最低温度=月温度;
}
如果(月>最高温度)
{
最高温度=月温度;
}
总计=总计+月数;
}
int totaltemperature=温度。长度;
平均温度=(总/总温度);
}
//打印结果
Label1.Text=string.Format(“最低温度为{0}
最高温度为{1}
平均温度为{2}
”,最低温度,最高温度,平均温度); }
您可以使用LINQ执行此操作:

var dict = new Dictionary<string, int>();
dict.Add("a", 3);
dict.Add("b", 4);
dict.Add("c", 5);
dict.Add("d", 6);

int value = 5; // or whatever
string key = dict.Where(kvp => kvp.Value == value)
                    .Select(kvp => kvp.Key)
                    .FirstOrDefault();
var dict=newdictionary();
添加(“a”,3);
添加(“b”,4);
添加(“c”,5);
添加(“d”,6);
int value=5;//或其他
字符串键=dict.Where(kvp=>kvp.Value==Value)
.Select(kvp=>kvp.Key)
.FirstOrDefault();

请记住,如果您有多个包含相同值的键,则可能会出现冲突问题。在这种情况下,您只需将
FirstOrDefault()
替换为
ToArray()
,即可获得值与给定值匹配的键数组。

您可以使用LINQ:

var dict = new Dictionary<string, int>();
dict.Add("a", 3);
dict.Add("b", 4);
dict.Add("c", 5);
dict.Add("d", 6);

int value = 5; // or whatever
string key = dict.Where(kvp => kvp.Value == value)
                    .Select(kvp => kvp.Key)
                    .FirstOrDefault();
var dict=newdictionary();
添加(“a”,3);
添加(“b”,4);
添加(“c”,5);
添加(“d”,6);
int value=5;//或其他
字符串键=dict.Where(kvp=>kvp.Value==Value)
.Select(kvp=>kvp.Key)
.FirstOrDefault();

请记住,如果您有多个包含相同值的键,则可能会出现冲突问题。在这种情况下,您只需将
FirstOrDefault()
替换为
ToArray()
获取具有与给定值匹配的值的键数组。

您正面临此问题,因为您的数据结构不足以胜任此工作。
字典[]
无法将其剪切。因此,请耐心听我说,并阅读此长答案

介绍您自己的类以将属性分组在一起。类
度量
包含数据

// single data point
public class Measurement {
    public string Day { get; set; }
    public int Temperature { get; set; }
}
类还可以封装计算。外部只消耗结果,因此您可以更改底层实现。重要的是,这将使您的代码更易于理解

Month
隐藏计算。计算是使用
ICollection
和实现的


完整示例:

您正面临此问题,因为您的数据结构不足以胜任此项工作。
字典[]
无法解决此问题。因此,请耐心听我说,并阅读此长答案

介绍您自己的类以将属性分组在一起。类
度量
包含数据

// single data point
public class Measurement {
    public string Day { get; set; }
    public int Temperature { get; set; }
}
类还可以封装计算。外部只消耗结果,因此您可以更改底层实现。重要的是,这将使您的代码更易于理解

Month
隐藏计算。计算是使用
ICollection
和实现的


完整示例:

以下是我试图简化的步骤。在文件开头:

using KVP = System.Collections.Generic.KeyValuePair<string, int>; 

似乎
字典
(每天的温度数组)更适合您的情况,但我使用了键值对数组来简化示例


ILookup
类似于
Dictionary
,其中每个键(温度)都有多个值(天)。

以下是我尝试使其更简单的方法。在文件开头:

using KVP = System.Collections.Generic.KeyValuePair<string, int>; 

似乎
字典
(每天的温度数组)更适合您的情况,但我使用了键值对数组来简化示例