可以使用Linq来查找值是否与较大字符串的一部分匹配?

时间:2013-12-17 20:53:14

标签: c# regex linq

我已经研究过RegEx&使用linq的SQLMatch但我似乎无法找到适用于我的情况的规则。我想要创建的是......

//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/

1 个答案:

答案 0 :(得分:6)

你几乎就在那里,应该是另一种方式。另外,使用LINQ Any。如果找到任何匹配项,则返回true:

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