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

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 145,860 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
231,198 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