C# Querying With LINQ Querying the BirdWatcher Data Extension Method

Alan Mills
Alan Mills
29,673 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
29,673 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,710 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
29,673 Points

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