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 trialAaron Selonke
10,323 PointsQuerying 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.
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);
}
}
}
2 Answers
Steven Parker
231,275 PointsWe'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:
SPOILER ALERT
return stringsToMatch.Any(item => source.Contains(item));
Aaron Selonke
10,323 PointsHi 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
10,323 PointsThank you, my friend, Steven Parker
Jason Anders
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsSorry 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.