Question

I need to print a list of countries that use dollars as a currency from a web service. the data comes form a class called country services which contains this tuple:

public static IEnumerable<Tuple<string, string, string, string>> GetCountryData()
{
    var countryService = new CountryServiceProxy.country();
    var xmlStringResult = countryService.GetCurrencies();

    var result = new List<Tuple<string, string, string, string>>();

    var xPathDoc = new XPathDocument(new XmlTextReader(new StringReader(xmlStringResult)));
    var navigator = xPathDoc.CreateNavigator();
    var nodes = navigator.Select("//Table");
    var nodeNames = new[] {"Name", "CountryCode", "Currency", "CurrencyCode"};

    while (nodes.MoveNext())
    {
        var nodeValues = new[] {string.Empty, string.Empty, string.Empty, string.Empty};
        for (var i = 0; i < nodeNames.Length; i++)
        {
            var node = nodes.Current.SelectSingleNode(nodeNames[i]);
            if (node != null)
            {
                nodeValues[i] = node.Value;
            }
        }

        result.Add(new Tuple<string, string, string, string>(nodeValues[0], nodeValues[1], nodeValues[2], nodeValues[3]));

    }

    return result;
}

I need to call that method and use it to print out a list with countries that use dollars:

private static IEnumerable<Country> Excercise4()
{
    //     var data = CountryService.GetCountryData().Where(x => x.Item3.Contains("Dollar"));
    //    ////var data = CountryService.GetCountryData().Where(x => x.Item3 == "Dollar");
    //    //Console.WriteLine(data);

    return new List<Country>
    {
         new Country("da", "C1", "$", "Dollar"),
         new Country("Country2", "C3", "$", "Dollar")
    };
}

so far my method looks like this. i cant seem to figure out how to print out the tuple , havent used tuple before.

my write method is as follows:

ConsoleHelper.PrintCountryResults(Excercise4(), "Return a list of Country objects who's currency is Dollar");
ConsoleHelper.Pause(PauseMessage);

and the console helper class looks like this :

public static void PrintCountryResults(IEnumerable<Country> results, string title)
{
    PrintHeader(title);
    foreach (var result in results)
    {
        Console.WriteLine("{0, -30}{1, -5}{2, -15}{3, -5}", result.CountryName, result.CountryCode, result.CurrencyName, result.CurrencyCode);
    }
}

any help would be appreciated as i said havent used tuple before first try at them .. Thanx

Was it helpful?

Solution

You can filter the properties of the tuple by referencing the ItemX property with X being the 1-based index of the property that you're interested in. in you case if the currency is the third item, filtering the tuples would look like

var countriesUsingDollarAsACurrency = GetCountryData().Where(tuple => tuple.Item3 == "Dollar");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top