Michael A
Michael A

Reputation: 9900

Is this kind of a statement possible?

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?

Upvotes: 0

Views: 72

Answers (3)

Usman Zafar
Usman Zafar

Reputation: 1979

File URL is case insensitive. So I think you should do:

if (contents.Any(c => fileUrl.IndexOf(c, StringComparison.InvariantCultureIgnoreCase) != -1))
{
    continue;
}

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166376

Try using Exists

Determines whether the specified array contains elements that match the conditions defined by the specified predicate.

Something like

if(Array.Exists(contents, s => fileUrl.Contains(s))) continue;

Upvotes: 1

zerkms
zerkms

Reputation: 254916

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

Explanation:

  1. http://msdn.microsoft.com/en-us/library/system.linq.enumerable.any.aspx - there is a .Any() method that LINQ provides you.

  2. It works as the following: it accepts a function reference (lambda in this case) which accepts one argument of the same type as your collection item (string in your case, I called it i) and must return a boolean. If there is true returned for some element - the whole .Any() returns true.

So basically it passes every item of the contents array as i into the fileUrl.Contains(i) expression until one returns true.

Upvotes: 5

Related Questions