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

I'm really struggling with a challenge. This looks right to me. Last Q in Linq :)

The goal is to see if any of stringsToMatch are in the source, to me that looks like stringsToMatch.Any preceeded by source.Contains, however I'm not having any luck with that.

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 => s.Contains(source));
        }
    }
}

2 Answers

Matthew Hill
Matthew Hill
7,799 Points

Try using .contains on your source string first ?

return source.Contains(stringsToMatch.Any(n => n.?)); Am I getting warmer?

Just finished it. This is what worked for me.

public static class ContainsAnyExtension {
        public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch) {
            return stringsToMatch.Any(item => source.Contains(item));  
        }
    }