ASP.NET C# - Modify value of a pair by reference

Using ASP.NET C#, how can I modify the value of a pair by reference instead of having to do a foreach?
            foreach (Pair pPair in pPairs)
            {
                if (pPair.pairCode == "EURUSD") {
                  pPair.pairSky = 5;
                }
            }

Open in new window

I would like to set the value of pairSky to 5 where the pairCode is "EURUSD" without the need of foreach.


using System;
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class MonkeyStaircase : Robot
    {
        List<Pair> pPairs = new List<Pair> {};

        protected override void OnStart()
        {
            string Pair_List = "USDCAD,EURUSD,USDCHF,GBPUSD,NZDUSD,AUDUSD,USDJPY,EURCAD,EURAUD,EURJPY,EURCHF,EURGBP,AUDCAD,GBPCHF,GBPJPY,CHFJPY,AUDJPY,AUDNZD";
            string[] Pairs = Pair_List.Split(',');
            foreach (string p in Pairs) {
              pPairs.Add(new Pair{pairCode = p, pairSky=0});
            }
        }

        protected override void OnTick()
        {
            foreach (Pair pPair in pPairs)
            {
                if (pPair.pairCode == "EURUSD") {
                  pPair.pairSky = 5;
                }
            }
        }

        public class Pair
        {
            public string pairCode { get; set; }
            public List<double> pairSpread = new List<double>();
            public double pairSky { get; set; }
        }
    }
}

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

try this

protected override void OnTick()
{
     pPairs.Where(c => c.pairCode == "EURUSD").ToList().ForEach(x => x.pairSky = 5);
}

Open in new window

skijAuthor Commented:
It looks like that is still using foreach.  Is there a more efficient way to change the value of pairSky where the pairCode is "EURUSD" without using foreach?
Lokesh B RDeveloperCommented:
Hi,

try this without foreach.

protected override void OnTick()
{
      pPairs.Where(c => c.pairCode == "EURUSD").Select(p => { p.pairSky = 5; return p; }).ToList();
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.