Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

StringCriterion issue

Vote:
 

ImageQuery imageQuery = new ImageQuery();

imageQuery.ImageGallery = new ImageGalleryCriterion();

imageQuery.ImageGallery.Header = new StringCriterion();

imageQuery.ImageGallery.Header.Value = "name";

 

This query works perfectly, but how do I exclude all the images that has "name" in it? Can't find it anywhere.

#43753
Sep 22, 2010 17:21
Vote:
 

Hi,

We are working with Relate 1, so I don't know if this applies to version 2, but the answer is that you have to write your own StringInCriterion. Below is our implementation of such an StringInCritertion used for excluding topic replies containing a certain word (haven't tried it for ImageQuery though).

/// <summary>
/// This class can be used in queries where a critera is set to exclude items containing for instance a specific word.
/// replyQuery.Text = new StringCriterion();
/// replyQuery.Text.Includes = new CustomWildCardStringInCriterion();
/// replyQuery.Text.Includes.Values.Add("dontWannaShowReplyCotainingThisWord");
/// </summary>
public class CustomWildCardStringInCriterion : StringInCriterion
{
    public CustomWildCardStringInCriterion()
    {
        this.WildCardType = WildCardType.Both;
    }

    public override string GetQuery(string propertyName)
    {
        if (Values != null && Values.Count > 0)
        {
            string parentPropertyName = ((CriterionBase)this.ParentQuery).AssignedPropertyPath;
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.Append("(");
            foreach (string val in base.Values)
            {
                if (queryBuilder.Length > 1)
                    queryBuilder.AppendFormat(" {0} ", QueryBase.OR_OPERATOR);
                queryBuilder.AppendFormat("{0} NOT LIKE {1}", parentPropertyName, this.FormatValue(val));
            }
            queryBuilder.Append(")");

            if (NullValueAction == NullValueAction.Include)
                queryBuilder.AppendFormat(" {0} {1} IS NULL", QueryBase.OR_OPERATOR, parentPropertyName);

            return queryBuilder.ToString();
        }
        else
            return string.Empty;
    }

    protected override string FormatValue(string val)
    {
        return String.Format("'{0}'", GetWildCardValue(val, this.WildCardType).Replace("'", "''"));
    }

    protected string GetWildCardValue(string val, WildCardType wildCardType)
    {
        if (wildCardType == WildCardType.None)
            return val;
        else
        {
            StringBuilder sb = new StringBuilder();

            if (wildCardType == WildCardType.Leading || wildCardType == WildCardType.Both)
                sb.Append(QueryBase.PERCENTAGE_OPERATOR);

            sb.Append(val);

            if (wildCardType == WildCardType.Trailing || wildCardType == WildCardType.Both)
                sb.Append(QueryBase.PERCENTAGE_OPERATOR);

            return sb.ToString();
        }
    }

    public virtual WildCardType WildCardType
    {
        get
        {
            return base.GetParameterValue<WildCardType>("WildCardType");
        }
        set
        {
            base.SetParameterValue<WildCardType>("WildCardType", value);
        }
    }
}

Haven

// Kind regards, Torbjörn

#47740
Feb 11, 2011 7:55
Vote:
 

This was really helpful. Maybe this criterion could be included in Framework, as Relate 2 hasn't this criterion either.. ?!

#56830
Feb 10, 2012 15:58
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.