This is the look-up class:
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);
}
}
From the interface, if the user enters A = 6000 B = 400 C = -5400 by B-A.
If C value is equal to the distance = -5400 from the LookUp calss then get the value distanceAngle = -220
and fill to the datagrid specific column from 1 to 5 cells.
If C value is eqaual to the distance = -5400 and get the value valueOfSideAngle = 320
and fill to the datagrid specific column from 6 to 10 cells.
This way I can get the column index : datagridview1.Columns["columnName"].Index;
This is the function, where I’m performing the calculation:
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;
}
How can I get the value from the lookup table which is equal to value3 and fill to the datagridview. Any help…!
Firstly you need two values(
side angleanddistance angle) from a single key that isdistancewhich requires some multidimensional collection. Anything with more than one value for a key gets complex and you should use a complex data structure only if it warrants. Lets look at the options:1) Your method:
Lookup<int, int>Lookup in this fashion is applicable only when you are having more entries with same key but different values. In other words, if you have:
And if you do:
you will get a lookup of Count 2 which means the keys are 5400 and 5000 but you will have two values for the key 5400. And the values will be in an
IEnumberable<int>. Hence you have two values in value collection for key 5400, but one value in theIEnumerable<int>value collection for key 5000. This is not what you are essentially looking for.What you need is something like
Collection<TKey, TValue1, TValue2>.2)
List<DistanceAngle>:I would say the best option is to go ahead with the list you have and create a function to get the
distance angleandside anglefrom distance. Since you said in your comments that the list wont be too large, you can create extension method or so, something like this:Now you can call:
3) If you need fast lookups you can rely on Dictionary (if there wont be duplicates) or Lookup (if there will be) but using something like
Dictionary<int, Tuple<int, int>>orLookup<int, Tuple<int, int>>will be cumbersome.4) You could do:
Dictionary<int, DistanceAngle>orLookup<int, DistanceAngle>(I’ll show Dictionary one, both of which are the same).
}
Now accessing by key is easy: