Can Linq be used to determine if the value of part of a larger string matches?

I looked at RegEx and SQLMatch with linq, but I cannot find the applicable rule for my situation. What I'm trying to create will be ...

//dataCollected[0] = { Name="joe", Url="http://my.home.site/" }
//dataCollected[N] = { Name="example", Url="http://german.home.site/" }

public bool hasParent(string test_url){
    var obj = dataCollected.Where(s => ( test_url.contains(s.Url)));
    return obj.Count() > 0;
}

bool  result  = hasParent("http://my.home.site/ShouldBeTrue"); //Finds http://my.home.site/
+4
source share
1 answer

You're almost there, it should be the other way around. Also use LINQ Any. This will return true if a match is found:

public bool hasParent(string test_url)
{
    return dataCollected.Any(s => test_url.Contains(s.url));
}
+6
source

All Articles