C# Querying With LINQ Querying the BirdWatcher Data Extension Method

David Merk
David Merk
9,863 Points

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,798 Points

Try using .contains on your source string first ?

David Merk
David Merk
9,863 Points

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));  
        }
    }