Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Querying With LINQ Now You're Querying Using Query Syntax

help please

Create a public method in the NumberAnalysis class called NumbersGreaterThanFive that returns an IEnumerable<int>. Inside the method, use LINQ query syntax to return only the numbers in the _numbers field that are greater than 5.

NumberAnalysis.cs
using System.Collections.Generic;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public class NumberAnalysis
    {
        private List<int> _numbers;
        public NumberAnalysis()
        {
            _numbers = new List<int> { 2, 4, 6, 8, 10 };
           IEnumerable<int>NumbersGreaterThanFive = from n in _numbers where n >5 select n;
        }
    }
}

3 Answers

Howdy!

Instead of creating a method you created an Enumerable with that name in the constructor. You should create a method that returns that Enumerable.

Good luck!

SPOILER

 public IEnumerable<int> NumbersGreaterThanFive()
{
      return from n in _numbers where n > 5 select n;           
}

thank you very much

Hello!

Here is how I did it.

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

namespace Treehouse.CodeChallenges
{
    public class NumberAnalysis
    {
        private List<int> _numbers;
        public NumberAnalysis()
        {
            _numbers = new List<int> { 2, 4, 6, 8, 10 };
        }

        public IEnumerable<int> NumbersGreaterThanFive()
        {
            return _numbers.Where(x => x > 5);
        }
    }
}
Allan Clark
Allan Clark
10,810 Points

First, you are required to create a new method for this task. You currently have your linq query in the constructor method. Being that it gives you the name and return type the method signature should look like this:

public IEnumerable<int> NumbersGreaterThanFive()

Your query looks correct so I will leave the implementation up to you!

Happy coding!!!

thank you