C# Querying With LINQ Querying the BirdWatcher Data Extension Method

Aaron Selonke
Aaron Selonke
10,323 Points

Querying With Linq Challange

Not sure why this isn't passing,

There was a hint in the bummer Message, but the longer I look at it, the more I want to punch the computer

Bummer! I used the ContainsAny method and expected true, but got false. The string I used it on was 'Woohoo!' and I passed 'Woo, hoo, oo' in the stringsToMatch parameter.

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

        }


    }
}
Jason Anders
Jason Anders
Treehouse Moderator 143,622 Points

Sorry Aaron,

I haven't done this course yet, so I don't feel qualified to provide an answer. Hopefully, someone else in the Community will be able to help you.

:dizzy:

2 Answers

Steven Parker
Steven Parker
155,231 Points

We're not looking to see if the string is found in the list, we want to check if the any list item is found in the string. So while there is a Contains method for lists, we want the one for strings, and use it for each of the items in the list.

If you're still stuck after that hint, here's how I did it:


:warning: SPOILER ALERT


            return stringsToMatch.Any(item => source.Contains(item));

:sparkles:

Aaron Selonke
Aaron Selonke
10,323 Points

Hi Steven, I put both of the codes in the IDE and tried both on a list, they get the same result...

A) not sure what the logical difference is between checking of the string is found on the list and checking if the list items are found in the string. Is there a difference?

B) return stringsToMatch.Any(item => source.Contains(item)); What is going on here with the Any() method?? The sequence will go into the item parameter of .Contains and return a true or a false to the any method? Not sure how to understand how the Contains() and Any() methods are working together here....

Aaron Selonke
Aaron Selonke
10,323 Points

Thank you, my friend, Steven Parker