从查找表中获取特定值?

时间:2012-11-04 12:04:49

标签: c# winforms datagridview lookup lookup-tables

这是查找课程:

public class DistanceAngle
{
    public int distance;
    public int distanceAngle;
    public int valueOfBoth;

    public static void getDisatnceAngleLookup()
    {
        List<DistanceAngle> distanceAngles = new List<DistanceAngle>
                {
                   new DistanceAngle{distance = -5400, distanceAngle = -220, valueOfSideAngle = 320},
                   new DistanceAngle{distance = -5200, distanceAngle = -210, valueOfSideAngle =  290},
                   new DistanceAngle{distance = -5000, distanceAngle = -200, valueOfSideAngle = 200},                                      
                };

        //Distance as Key Value and look for distanceAngle
        Lookup<int, int> lookup = (Lookup<int, int>)distanceAngles.ToLookup((p => p.distance),
                                                                    p => p.distanceAngle);

        int count = lookup.Count;

        // Select a collection of distanceAngles by indexing directly into the Lookup.
        IEnumerable<int> cgroup = lookup[-5400];

        // Output the results.
        Console.WriteLine("\nPackages that have a key of 'C':");
        foreach (int str in cgroup)
            Console.WriteLine(str);
    }
}

如果用户通过A = 6000 B = 400 C = -5400输入B-A,则从界面开始。 如果C值等于LookUp calss中的distance = -5400,则获取值distanceAngle = -220 并从1到5个单元格填充到datagrid特定列。

如果C值与distance = -5400相同,则获取值valueOfSideAngle = 320 并从6到10个单元格填充到datagrid特定列。

这样我就可以得到列索引:datagridview1.Columns["columnName"].Index;

这是我正在执行计算的函数:

private void b_calculate_Click(object sender, EventArgs e)
    {

        int value1;
        int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                         CultureInfo.InvariantCulture.NumberFormat, out value1);

        int value2;
        int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                         CultureInfo.InvariantCulture.NumberFormat, out value2);

        var value3 = value2 - value1;


    }

如何从查找表中获取等于value3的值并填充到datagridview。任何帮助......!

1 个答案:

答案 0 :(得分:2)

首先,您需要来自side angle的单个键的两个值(distance angledistance),这需要一些多维集合。对于密钥具有多个值的任何内容都会变得复杂,只有在保证时才应使用复杂的数据结构。让我们看看选项:

1)您的方法:Lookup<int, int>

以这种方式查找仅适用于具有相同键但值不同的更多条目的情况。换句话说,如果你有:

List<DistanceAngle> distanceAngles = new List<DistanceAngle>
{
   new DistanceAngle{distance = -5400, distanceAngle = -220},
   new DistanceAngle{distance = -5400, distanceAngle = -210},
   new DistanceAngle{distance = -5000, distanceAngle = -200}
};

如果你这样做:

var lookup = (Lookup<int, int>)distanceAngles.ToLookup(p => p.distance, 
                                                       p => p.distanceAngle);

您将获得Count 2的查找,这意味着键为5400和5000但您将为键5400提供两个值。值将在IEnumberable<int>中。因此,键值5400的值集合中有两个值,但键值5000的IEnumerable<int>值集合中有一个值。这不是您实际需要的值。

您需要的是Collection<TKey, TValue1, TValue2>之类的内容。

2)List<DistanceAngle>

我想说最好的选择是继续使用你拥有的列表并创建一个函数来远离distance angleside angle。既然你在评论中说列表不会太大,你可以创建扩展方法,如下所示:

public static int GetDistanceAngle(this List<DistanceAngle> distanceAngles, 
                                   int distance)
{
    var d = distanceAngles.FirstOrDefault(d => d.distance == distance);
    if (d == null)
        return 0; //your value
    return d.distanceAngle;
}

public static int GetSideAngle(this List<DistanceAngle> distanceAngles, 
                               int distance)
{
    var d = distanceAngles.FirstOrDefault(d => d.distance == distance);
    if (d == null)
        return 0; //your preferred value
    return d.sideAngle;
}

现在你可以致电:

private void b_calculate_Click(object sender, EventArgs e)
{
    int value1;
    int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value1);

    int value2;
    int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value2);

    var value3 = value2 - value1;

    var sideAngle = distanceAngles.GetSideAngle(value3);
    var distanceAngle = distanceAngles.GetDistanceAngle(value3);
    //etc
}

3)如果你需要快速查找,你可以依靠Dictionary(如果不重复)或Lookup(如果有),但使用像Dictionary<int, Tuple<int, int>>Lookup<int, Tuple<int, int>>这样的东西会很麻烦。

4)你可以这样做:Dictionary<int, DistanceAngle>Lookup<int, DistanceAngle>

(我将显示词典一,两者都相同)。

public static Dictionary<int, DistanceAngle> getDisatnceAngleLookup()
{
    List<DistanceAngle> distanceAngles = new List<DistanceAngle>
    {
       new DistanceAngle{distance = -5400, distanceAngle = -220, valueOfSideAngle = 320},
       new DistanceAngle{distance = -5200, distanceAngle = -210, valueOfSideAngle =  290},
       new DistanceAngle{distance = -5000, distanceAngle = -200, valueOfSideAngle = 200},                                      
    };

    //Distance as Key Value and look for distanceAngle
    return distanceAngles.ToDictionary(p => p.distance, p => p);
}

}

现在按键访问很简单:

private void b_calculate_Click(object sender, EventArgs e)
{
    int value1;
    int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value1);

    int value2;
    int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value2);

    var value3 = value2 - value1;

    var sideAngle = distanceAngles[value3].sideAngle;
    var distanceAngle = distanceAngles[value3].distanceAngle;
    //etc
}