Avatar of samelamin
samelamin
 asked on

find value in list

hi i have a list populated with values and would like to return a value

but the string returned is a

System.Linq.Enumerable+WhereSelectListIterator`2[ConversionApp.ExchangeRates,System.Char]


how do i get the actual value?
var variable = (from l in ratesList
                              where l.ISO.Contains(exchangeISO1)
                              select l.Closing.Single());



             test = variable.ToString(); ;

Open in new window

C#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
BourbonKid

Your variable is an iterator on a selection list. If you want to retrieve a ConversionApp.ExchangeRates object you could use :


ConversionApp.ExchangeRates exchangeRate = ((IEnumerator<ConversionApp.ExchangeRates>)variable).Current;

Open in new window


After that all will depend whether your ExchangeRates class defines an overloaded version of ToString() or not. If not, you will have either to define such a method if you can modify the class or define your own helper method to build the string representation from appropriate object properties and methods.

Nota 1 : the object you retrieve is an enumerator. Hence, it may allow for retrieval of several ExchangeRates instances. You will have to figure out which one is of interest for you.

Nota 2 : The Current property in the code sample may be a null reference. You should handle the case appropriately.
Dirk Haest

Do you just want to retrieve the first one you'll find ?
List<Rate> ratesList = new List<Rate>();
            ratesList.Add(new Rate("EUR", "1.0"));
            ratesList.Add(new Rate("USD", "2.5"));
            ratesList.Add(new Rate("CHF", "3.8"));

            string exchangeISO1 = "USD";

            var variable = (from l in ratesList
                            where l.ISO.Contains(exchangeISO1)
                            select l.Closing).FirstOrDefault<string>();

            if (string.IsNullOrEmpty(variable))

Open in new window

kaufmed

Try this--move the call to Single outside of the parentheses:
var variable = (from l in ratesList
                where l.ISO.Contains(exchangeISO1)
                select l.Closing).Single();

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
samelamin

ASKER
that would work but its returning a variable rather than the string
kaufmed

that would work but its returning a variable rather than the string
A) To whom are you referring?
B) Can you elaborate on "returning a variable"?
samelamin

ASKER
i cant just set the value of the string to variable "test" because i get this error

System.Linq.Enumerable+WhereSelectListIterator`2[ConversionApp.ExchangeRates,System.Char]


Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
samelamin

ASKER
sorry your right kaufmed Thanks for your help
samelamin

ASKER
brilliant
kaufmed

NP. Glad to help  = )
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck