Link to home
Start Free TrialLog in
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

Avatar of BourbonKid
BourbonKid
Flag of United States of America image

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.
Avatar of 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

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

Avatar of samelamin
samelamin

ASKER

that would work but its returning a variable rather than the string
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"?
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]


ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry your right kaufmed Thanks for your help
brilliant
NP. Glad to help  = )