C# Querying With LINQ Now You're Querying Selecting, Projecting and Anonymous Types

Carel Du Plessis
PRO
Carel Du Plessis
Pro Student 16,356 Points

I don't quite understand why my code doesn't display values.

I initialized a List with the bird object as the data type so that I can go through it and display the values by using the foreach loop.

the code that doesn't work I have commented out.

// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ex_LINQ
{
    class Program
    {
        static void Main(string[] args)
        {

            // This doesn't work

            //List<Bird> birds = new List<Bird>();

            //foreach (Bird bird in birds)
            //{
                 //Console.WriteLine(bird.Sightings);
            // }



             //This  works??? 

            List<Bird> birds = BirdRepository.LoadBirds();

            foreach (Bird bird in birds){
                Console.WriteLine();
                Console.WriteLine("Bird Name: " + bird.Name);
                Console.WriteLine("Bird Color: " + bird.Color);
                Console.WriteLine("Bird Sightings: " + bird.Sightings);
            }

            Console.ReadKey();
        }
    }
}


// Bird.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ex_LINQ
{
    public class Bird
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public int Sightings { get; set; }
    }
}



// BirdRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ex_LINQ
{
    public static class BirdRepository
    {
        public static List<Bird> LoadBirds()
        {
            //Refactor the LoadBirds method by using object initialization to add the pelican to the birds variable.

            List<Bird> birds = new List<Bird>{
                new Bird {Name = "Pelican", Color = "White", Sightings = 3}
            };
            return birds;
        }
    }
}

2 Answers

For the commented code, you have a list of birds that you have initialized but have no value. So Console.WriteLine has nothing to write since the bird.Sightings property is empty. look more at your BirdRepository class

I took elements of the code an came up with this is works. It can be also be checkout here

using System;
using System.Collections.Generic;
using System.Linq;


namespace LINQStuff
{
    class Program
    {

        static void Main()
        {

            var birds = new List<Bird>
            {
                new Bird { Name = "Cardinal", Color = "Red", Sightings = 3 },
                new Bird { Name = "Dove", Color = "White", Sightings = 2 } ,
                new Bird { Name = "Robin", Color = "Red", Sightings = 5 } ,
                new Bird { Name = "Blue Jay", Color = "Blue", Sightings = 1 } ,
            };

            var canary = new Bird { Name = "Canary", Color = "Yellow", Sightings = 0 };
            birds.Add(canary);


            foreach (Bird bird in birds)
            {

                Console.WriteLine();
                Console.WriteLine($" Bird Name: {bird.Name} \n " +
                    $"Bird Color: {bird.Color} \n " +
                    $"Bird Sightings: {bird.Sightings} " );
            }


            Console.ReadLine(); // this takes out the line "press any key to continue"

        }
    }
}