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+Whe reSelectLi stIterator `2[Convers ionApp.Exc hangeRates ,System.Ch ar]
how do i get the actual value?
but the string returned is a
System.Linq.Enumerable+Whe
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(); ;
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))
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();
ASKER
that would work but its returning a variable rather than the string
that would work but its returning a variable rather than the stringA) To whom are you referring?
B) Can you elaborate on "returning a variable"?
ASKER
i cant just set the value of the string to variable "test" because i get this error
System.Linq.Enumerable+Whe reSelectLi stIterator `2[Convers ionApp.Exc hangeRates ,System.Ch ar]
System.Linq.Enumerable+Whe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry your right kaufmed Thanks for your help
ASKER
brilliant
NP. Glad to help = )
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.