2

I currently have a an ASP.Net Website. This website retrieves RSS-Feeds from different Sources and outputs its own RSS-Feed. But is the article contains some word they must be 'blacked out' (Censorship as you might say). The words are contained in a blacklist managed by that ASP.Net Website.

My current solution:

foreach (SyndicationItem rssItem in syndFeed.Items.OrderByDescending(x => x.PublishDate).ToList())
{
    //if is on blacklist
    bool isValid = true;
    foreach (String blacklistItem in MyBlacklist)
    {
        if(rssItem.Title.Text.contains(blacklistItem))
            isValid = false;
        if(rssItem.Summary.Text.contains(blacklistItem))
            isValid = false;
    }

    if (isValid)
    {
        writer.WriteStartElement("item");

        //Write Elements
        writer.WriteElementString("PubDate", rssItem.PublishDate.ToString("yyyy-MM-dd HH:mm:ss"));
        writer.WriteElementString("title", rssItem.Title.Text);
        writer.WriteElementString("description", rssItem.Summary.Text);
        writer.WriteElementString("link", rssItem.Id.ToString());

        writer.WriteEndElement();
    }
}

Question: Is there a more performant way to do this search? How can i write the following code more simple using LINQ/ Any other Stringoperations?

    foreach (String blacklistItem in MyBlacklist)
    {
        if(rssItem.Title.Text.contains(blacklistItem))
            isValid = false;
        if(rssItem.Summary.Text.contains(blacklistItem))
            isValid = false;
    }

Based on the following examples from Java:

Checking if a string contains any of the strings in an array

How to check string with array of strings in java?

Test if a string contains any of the strings from an array

1 Answer 1

4
bool isValid = !myBlackList.Any(s=> rssItem.Title.Text.contains(s) || rssItem.Summary.Text.Contains(s))

Or

bool isValid = myBlackList.All(s=> !rssItem.Title.Text.Contains(s) && !rssItem.Summary.Text.Contains(s))
Sign up to request clarification or add additional context in comments.

1 Comment

Oh God! how could i have missed this? Your solution works like a charm! Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.