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 Querying the BirdWatcher Data Extension Method

Alan Mills
Alan Mills
31,712 Points

struggling with the syntax.

It sounds like I need to pull an any to provide the bool result. but I'm struggling to come up with syntax that doesn't give off errors. Hints greatly appreciated. Thanks !!

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

namespace Treehouse.CodeChallenges
{
    public static class ContainsAnyExtension
    {
        public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
        {
            return stringsToMatch.Any(s => source.Contains(stringsToMatch));
        }
    }
}
Alan Mills
Alan Mills
31,712 Points

Another attempt that I've also tried: return stringsToMatch.Any(stringsToMatch.Contains(source));

this would feel like the scope of what needs to happen. But I'm confident I've flubbed the syntax.

1 Answer

Samuel Ferree
Samuel Ferree
31,722 Points

think of your lambda like a function, and the s is what you pass it. As the "Any" function loops through each string, it will pass each one into your function as s. So what you want to do is check if anything passed in as s is equal to source

//first as a foreach loop
foreach (string s in stringsToMatch)
{
  if(source.equals(s))
  {
    return true;
  }
}

Try this:

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

namespace Treehouse.CodeChallenges
{
    public static class ContainsAnyExtension
    {
        public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
        {
            //on each pass, we're comparing a string s, from stringsToMatch against source
            return stringsToMatch.Any(s => source.equals(s));
        }
    }
}
Alan Mills
Alan Mills
31,712 Points

Looks like I need to review the Lambda & extension sections. Thanks again!