简体   繁体   中英

Assign values to 2d array and getting the output

I am working on a Windows Forms Application and am using C#, entity framework. i have two tables in database tracks and defended points.

i am trying use foreach to loop those two tables and do some equations and assign the output of the equations(_DATIX) to 2d array. Here is the NOT WORKING code I tried:

double[,] arr = new double[db.track_776.Count(), db.defendedpoints.Count()];

foreach (var track in db.track_776.ToList())
{
    foreach (var defendedPoint in db.defendedpoints.ToList())
    {

        DELATX = ((double)defendedPoint.Long - (double)track.TRK_LongX) * 30.8;
        DELATY = ((double)defendedPoint.Lat - (double)track.TRK_LatY) * 27;

        D = (int)Math.Sqrt((DELATX * DELATX) + (DELATY * DELATY));
        double x = (double)track.XVelocity;
        double y = (double)track.YVelocity;
        S = (int)Math.Sqrt((x * x) + (y * y));

        _DATIX = CalculateDefendedAreaThreatIndex((int)track.Alt,
            (int)defendedPoint.DpAlt,
            (int)defendedPoint.Priority, (int)defendedPoint.Range, DELATX, DELATY, D, S, x, y);

        arr[db.track_776.Count(), db.defendedpoints.Count()] = _DATIX;

the output is 0 for all DATIX.(0 IS the initial Value for DATIX)

My loyal friend suggested this solution to me and it worked. note that ( he is an entity frame work expert) '''

  double[,] arr = new double[db.track_776.Count(), db.defendedpoints.Count()];

        for (int i = 0; i < db.track_776.Count(); i++)
        {
            for (int j = 0; j < db.defendedpoints.Count(); j++)
            {
                DELATX = ((double)db.defendedpoints.ToList()[j].Long - (double)db.track_776.ToList()[i].TRK_LongX) * 30.8; // *33 to convert from second to meter
                DELATY = ((double)db.defendedpoints.ToList()[j].Lat - (double)db.track_776.ToList()[i].TRK_LatY) * 27; // *33 to convert from second to meter
                D = (int)Math.Sqrt((DELATX * DELATX) + (DELATY * DELATY));
                double x = (double)db.track_776.ToList()[i].XVelocity;
                double y = (double)db.track_776.ToList()[i].YVelocity;
                S = (int)Math.Sqrt((x * x) + (y * y));

                _DATIX = CalculateDefendedAreaThreatIndex((int)db.track_776.ToList()[i].Alt, (int)db.defendedpoints.ToList()[j].DpAlt,
                    (int)db.defendedpoints.ToList()[j].Priority, (int)db.defendedpoints.ToList()[j].Range, DELATX, DELATY, D, S, x, y);
                arr[i, j] = DATIX;
            }
        }

'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM