0

I have the following:

var contents = new[]
    {
        "Theme Gallery", "Converted Forms", "Page Output Cache", "Master Page",
        "(no title)", ".css", ".xml", ".wsp", ".jpg", ".png", ".master"
        , ".000" , "Page Output Cache" , "_catalogs", "Style Library", "Cache Profiles"
    };

string fileUrl = siteCollectionEntity.Url + '/' + item.Url;

And I'd like to write a statement that will prevent a loop from continuing if a URL contains any of the words found in contents. Apologies for the following poor syntax, but I think it will help to explain better what I'm trying to do... Basically something like the following:

if (fileUrl.Contains(contents)) continue;

Is something like this possible?

4

3 回答 3

5
var contains = contents.Any(i => fileUrl.Contains(i));

解释:

  1. http://msdn.microsoft.com/en-us/library/system.linq.enumerable.any.aspx - 有一种.Any()方法可以LINQ为您提供。

  2. 它的工作原理如下:它接受一个函数引用(在本例中为 lambda),该引用接受一个与您的集合项相同类型的参数(string在您的情况下,我称之为它i)并且必须返回一个布尔值。如果true返回某个元素 - 整体.Any()返回 true。

contents所以基本上它将数组的每一项都传递ifileUrl.Contains(i)表达式中,直到返回true

于 2013-10-02T04:59:32.853 回答
1

尝试使用存在

确定指定数组是否包含与指定谓词定义的条件匹配的元素。

就像是

if(Array.Exists(contents, s => fileUrl.Contains(s))) continue;
于 2013-10-02T05:00:14.173 回答
0

文件 URL 不区分大小写。所以我认为你应该这样做:

if (contents.Any(c => fileUrl.IndexOf(c, StringComparison.InvariantCultureIgnoreCase) != -1))
{
    continue;
}
于 2013-10-02T05:13:07.800 回答