gpt4 book ai didi

c# - 从查找表中获取特定值?

转载 作者:行者123 更新时间:2023-11-30 17:09:08 24 4
gpt4 key购买 nike

这是查找类:

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);
}
}

在界面中,如果用户通过 B-A 输入 A = 6000 B = 400 C = -5400。如果 C 值等于来自查找调用的 distance = -5400,则获取值 distanceAngle = -220并填充 1 到 5 个单元格的数据网格特定列。

如果 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。任何帮助...!

最佳答案

首先,您需要来自单个键的两个值( side angledistance angle ) distance这需要一些多维集合。任何具有多个键值的事物都会变得复杂,您应该仅在必要时才使用复杂的数据结构。让我们看看选项:

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);

您将查找计数 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>> 的东西或 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
}

关于c# - 从查找表中获取特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13218357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com