如何使用C#中的字典和数组函数从表中访问值

时间:2018-06-07 12:54:19

标签: c# arrays dictionary

我试图在C#中编写一些代码,允许我在提供以下输入后访问下表中的值:

table

  • 地面(岩石,硬土,软土)

  • 时刻幅度(6.5,7.5,8.5)

  • source_to_source(0-20,20-50,50-100)

我已尝试使用以下代码,但不断收到消息:

  

发生System.Collections.Generic.KeyNotFoundException - '字典中没有给定的密钥'。

任何人都可以帮助我让它发挥作用吗?有没有更有效的方式来编写这段代码?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20180607_dict_example1
{
    class Program
    {
        static void Main(string[] args)
        {                            
            string ground = "Rock";
            string moment_magnitude = "6.5";
            string source_to_source = "0-20";            
            double ratio_peak;
            int first_value;
            int second_value;
            int third_value;                

            // 0b. Calculate ratio peak from Table 2 in Hashash paper

            var valueDict = new Dictionary<string, int> { { "6.5", 0 }, { "7.5", 1 }, { "8.5", 2 }, { "rock", 0 }, { "stiff soil", 1 }, { "soft soil", 2 }, };

            if (valueDict.ContainsKey(moment_magnitude))
            {
                first_value = valueDict[moment_magnitude];
                Console.WriteLine(first_value);
            }

            if (valueDict.ContainsKey(ground))
            {
                second_value = valueDict[ground];
                Console.WriteLine(second_value);
            }

            int[,] array = new int[3, 3] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };

            Console.WriteLine(array[valueDict[ground], valueDict[moment_magnitude]]);

            var valueDict_source_to_source = new Dictionary<string, int> { { "0-20", 0 }, { "20-50", 1 }, { "50-100", 2 } };

            if (valueDict_source_to_source.ContainsKey(source_to_source))
            {
                third_value = valueDict_source_to_source[source_to_source];
                Console.WriteLine(third_value);
            }

            int[,] ratios = new int[3, 9] { { 66, 97, 127, 94, 140, 180, 140, 208, 269 }, { 76, 109, 140, 102, 127, 188, 132, 165, 244 }, { 86, 97, 152, 109, 155, 193, 142, 201, 251 } };

            Console.WriteLine(ratios[valueDict_source_to_source[source_to_source], array[valueDict[ground], valueDict[moment_magnitude]]]);

            ratio_peak = (ratios[valueDict_source_to_source[source_to_source], array[valueDict[ground], valueDict[moment_magnitude]]]);

            Console.WriteLine(ratio_peak);

            Console.ReadKey();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

这是一个使用更面向对象方法的想法。

首先,创建一个代表每条记录的类,例如:

public class Surface
{
    /// <summary>
    /// Name of the surface e.g. RockA
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Moment magnitude Mw
    /// </summary>
    public double Moment { get; set; }

    /// <summary>
    /// Source to site distance from 0 to 20 km
    /// </summary>
    public int SourceToSite20 { get; set; }

    /// <summary>
    /// Source to site distance from 20 to 50 km
    /// </summary>
    public int SourceToSite50 { get; set; }

    /// <summary>
    /// Source to site distance from 50 to 100 km
    /// </summary>
    public int SourceToSite100 { get; set; }
}

然后创建它们的列表,确保每个组的表面名称相同,例如,RockA:

List<Surface> surfaces = new List<Surface>();
surfaces.Add(new Surface
{
    Name = "RockA",
    Moment = 6.5,
    SourceToSite20 = 18,
    SourceToSite50 = 23,
    SourceToSite100 = 30
});

surfaces.Add(new Surface
{
    Name = "RockA",
    Moment = 7.5,
    SourceToSite20 = 43,
    SourceToSite50 = 56,
    SourceToSite100 = 68
});

surfaces.Add(new Surface
{
    Name = "Stiff soil",
    Moment = 6.5,
    SourceToSite20 = 35,
    SourceToSite50 = 41,
    SourceToSite100 = 48
});

[...]

现在您可以更轻松地访问数据,例如,使用Linq查询:

获取表面为“RockA”的所有记录:

List<Surface> rocks = surfaces.Where(x => x.Name == "RockA").ToList();

力矩= 6.5的表面:

List<Surface> magintude65 = surfaces.Where(x => x.Moment == 6.5).ToList();

来自25到55之间的距离

List<Surface> result = surfaces.Where(x => x.SourceToSite50 >= 25 && x.SourceToSite100 <= 55).ToList();

如果您不想创建类,还可以使用元组列表:

var surfaces = new List<Tuple<string, double, int, int, int>>();
surfaces.Add(new Tuple<string, double, int, int, int>("RockA", 6.5, 18, 23, 30));
[...]

然后你可以进行同样的查询,但我建议你使用一个类,无论如何这都是它们的用途。

答案 1 :(得分:1)

默认情况下,带字符串键的字典使用默认的字符串比较器,它区分大小写。如果您将地面变量值更改为&#34; rock&#34;而不是&#34; Rock&#34;。