3

这是查找类:

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 值等于distance = -5400LookUp calss 中的值,则获取该值distanceAngle = -220 并从 1 到 5 个单元格填充到 datagrid 特定列。

如果 C 值等于distance = -5400并获取值valueOfSideAngle = 320 并填充到从 6 到 10 个单元格的数据网格特定列。

这样我可以获得列索引: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 的值。任何帮助...!

4

1 回答 1

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 的值集合中有两个值,但在IEnumerable<int>键 5000 的值集合中有一个值。这不是您真正要寻找的。

你需要的是类似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>>or的东西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
}
于 2012-11-04T15:36:59.583 回答