Search code examples
stringloopsrazorumbracocontains

Umbraco Razor, comparing 2 sets of page ID's in a string for any matches


was originally using .Contains() when my 2 comparison items were something like, Is:

1234

In:

5555 6732 **1234** 3456

And in this case, yes it was, and the code would run on the if(), but when I introduced multiple comparisons, like,

Is:

**1234** 2344 2341 3245

In:

5555 6732 **1234** 3456

It no longer works with .Contains(), so I am looking to find out how best to compare the 2 strings for any match, and if any block of four numbers is in both strings do X.

The blocks of numbers are currently in one string each, like 'placeIDs' and 'adIDs' with spaces in between each number.

Here is my full code if you prefer to see it:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using RJP.MultiUrlPicker.Models;
@{
string pagesID = "";
string adPlace = "";
var selection = Umbraco.TypedContent(1384).Children("headerAdvert").Where(x => x.IsVisible() && x.GetPropertyValue<bool>("active"));
    foreach(var item in Model.Content.Ancestors().OrderBy(x => x.Level))
        {
            pagesID = string.Concat(@item.Id.ToString(),"  ",pagesID);
        }
            pagesID = string.Concat(@Model.Content.Id.ToString(),"  ",pagesID);
            @:Page ID's @pagesID
     }

<div id="leaderboard" class="advert-carousel owl-carousel owl-theme" style="opacity: 1; display: block;">
    <div class="owl-wrapper-outer">
<div class="headerSlick owl-wrapper">
    @{
    foreach(var item in selection){
        var dateRange = item.GetPropertyValue<Diplo.DateRangePicker.DateRange>("campaignDate");
        <p>Your date range is from @dateRange.StartDate to @dateRange.EndDate</p>
        var typedMultiNodeTreePicker = item.GetPropertyValue<IEnumerable<IPublishedContent>>("location");
        foreach (var item2 in typedMultiNodeTreePicker)
            {
                adPlace = string.Concat(@item2.Id.ToString(),"  ",adPlace);
            }

        @:ad target ID's @adPlace
        if(item.HasValue("location") && pagesID.Contains(adPlace.ToString())){
        <script>alert("yes")</script>
        var singleLink = item.GetPropertyValue<Link>("links");
            if(singleLink != null)
            {
                if(item.HasValue("wideImage"))
                    {  
                    IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                        <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target">
                            <img class="tag-headadvert img-responsive" style="display: inline;" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a>
                        </div> 
                    }
            }
        }

        else if (item.IsNull("location")){
        var singleLink = item.GetPropertyValue<Link>("links");
            if(singleLink != null)
            {
                if(item.HasValue("wideImage"))
                    {  
                    IPublishedContent mediaItem = item.GetPropertyValue<IPublishedContent>("wideImage");
                       <div class="owl-item" style="width: 891px;"><a href="@singleLink.Url" target="@singleLink.Target"><img class="img-responsive" src="@mediaItem.Url" width="100%" alt="@singleLink.Name" /></a></div> 
                    }
            }
        }
    }
}
</div>
</div>
</div>

But in summary, I take the page's ID and all its parent ID's, make them into a string, then I take the Ad Location ID's make them into a string, and im trying to find if one ID exists in the other. I am happy to rework anything, but I am a beginner to this so please be patient. ;-)

Cheers


Solution

  • Welcome to StackOverflow :)

    If you put your strings into an Array, you can run Contains on them. Since you know your entries are always going to be separated by a space inbetween, you can simply do the following:

    var allMyAdIds = "1234 2387 1931 2313 8237 2391 3911 2833 9741 1234";
    var adIDs = allMyAdIds.Split(' ');
    if (adIDs.Contains("1234"))
    {
        // DO X
    }
    

    For multiple Contains, you could do:

    var targetsToMatch = new[]{"1234", "7851", "9138"};
    foreach (var item in targetsToMatch) {
        if (adIDs.Contains(item)) {
            // DO X
        }
    }