foreach over anonymous types

17,138

Solution 1

Your array needs to be of the type of your stock quote. Your stock quote is an anonymous type, so we need to define the array anonymously as well. This can be done cleanly as:

    var quotes = new[]{  new { Stock = "DELL", Quote = "123" },
                         new { Stock = "MSFT", Quote = "123" },
                         new { Stock = "GOOG", Quote = "123" }};

    foreach (var item in quotes)
    {
        Console.WriteLine(item.Stock);
        Console.WriteLine(item.Quote.ToString());
    }

Solution 2

i guess the problem is in that line:

var quotes = new object[] { quote1, quote2, quote3 };

quotes is a object array, not an array of that anonymous type. the foreach also has just the object value. you could try to form the array within one line or within a lambda expression

A very dirty workaround is changing 'var' to 'dynamic'

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new object[] { quote1, quote2, quote3 };

        foreach (dynamic item in quotes)
        {
            var r = item.Stock;

        }

a cleaner solution is leaving out 'object', so the compiler can generate an anonymous typed array

        var quote1 = new { Stock = "DELL", Quote = ("DELL") };
        var quote2 = new { Stock = "MSFT", Quote = ("MSFT") };
        var quote3 = new { Stock = "GOOG", Quote = ("GOOG") };

        var quotes = new [] { quote1, quote2, quote3 };

        foreach (var item in quotes)
        {
            var r = item.Stock;

        }
Share:
17,138
dannyrosalex
Author by

dannyrosalex

.NET developer

Updated on July 03, 2022

Comments

  • dannyrosalex
    dannyrosalex almost 2 years
    using System;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    
    namespace LearningJustCode
    {
        class Program
        {
            static void Main(string[] args)
            {
                Update();
            }
    
            static void Update()
            {
                var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
                var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
                var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };
    
                var quotes = new object[] { quote1, quote2, quote3 };
    
                foreach (var item in quotes)
                {
                    Console.WriteLine(item.Stock);
                    Console.WriteLine(item.Quote.ToString());
                }
            Console.ReadKey();
    
            }
    
            static string GetQuote(string stock)
            {
                try
                {
                    return InnerGetQuote(stock);
                }
                catch (Exception ex)
                {
    
                    Console.WriteLine(ex.Message);
                    return "N/A";
                }
            }
    
            static string InnerGetQuote(string stock)
            {
                string url = @"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
                var request = HttpWebRequest.Create(string.Format(url, stock));
    
                using (var response = request.GetResponse())
                {
                    using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                    {
                        return reader.ReadToEnd();
                    }
                }
    
    
            }
        }
    }
    

    I am getting an squiggley over item;Variable 'item' is only assigned to. This code has been slightly modified from Paul Kimmel's book C# Unleashed by Sams.