ASP.NET / C# / LINQ: Select By String

This works:
Print( pPairs[4].pairCode );

Open in new window

But this does NOT work:
Print( pPairs.Where(p => p.pairCode == "EURJPY").pairCode );

Open in new window

How can I select where pairCode == "EURJPY"?

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class MonkeyStaircase : Robot
    {
        public string Pair_List = "USDCAD,EURUSD,USDCHF,GBPUSD,NZDUSD,AUDUSD,USDJPY,EURCAD,EURAUD,EURJPY,EURCHF,EURGBP,AUDCAD,GBPCHF,GBPJPY,CHFJPY,AUDJPY,AUDNZD";
        List<Pair> pPairs = new List<Pair> {};

        protected override void OnStart()
        {
            string[] Pairs = Pair_List.Split(',');
            foreach (string p in Pairs) {
              pPairs.Add(new Pair{pairCode = p});
            }
            Print( pPairs[4].pairCode );
            Print( pPairs.Where(p => p.pairCode == "EURJPY").pairCode );
        }

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

    }
}

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.

p_davisCommented:
i think that you would have to do that with a foreach since the where can return many results
p_davisCommented:
or use firstordefault from lync
käµfm³d 👽Commented:
I agree with the above. Just keep in mind that FirstOrDefault will return null if it doesn't find a match, so you'll need to account for that if there's a chance that your collection won't contain that item.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Fernando SotoRetiredCommented:
Hi skij;

I do not know what Print is but must be one of your functions that will iterate over the results of the query. To return a Pair object/colection of Pair the query needs to be like the following. Just remove .pairCode you have at the end, doesn't belong there.

pPairs.Where(p => p.pairCode == "EURJPY")

Open in new window

If you are trying to return only one Pair object then add the FirstOrDefault method to the query as follows.
pPairs.Where(p => p.pairCode == "EURJPY").FirstOrDefault()

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
käµfm³d 👽Commented:
@Fernando

I believe FirstOrDefault was already mentioned. Aside from that, the Where is redundant. You can pass a predicate directly to FirstOrDefault:

pPairs.FirstOrDefault(p => p.pairCode == "EURJPY")
Fernando SotoRetiredCommented:
Hi käµfm³d;

Yes that is correct but I was correcting skij query to make it work and if he just wanted only one Pair object giving the added method to the already correct statement.
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.