My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and ...

Get unique value from Array (String/Integer):

Problem: In one of my project, I am getting values from one datasource in the form of .net Array. In the .NET Framework 3.5 there is a function called Distinct(). This Distinct() function return unique values from Array. This function works fine in the case of integer array. In string array, it not able to handle case sensitivity. If all items in the string array in same case, then it will work fine. Due to this, I was facing problem in getting unique value from String Array. Resolution: To Solve this problem, I have created functions, which used Stringcomparison. InvariantCultureIgnoreCase. .NET Framework 3.5: It is one line of code.
//If integer array
int [] intArr={5,2,3,3,5};
intArr = intArr.Distinct().ToArray(); // Output : intArr ={5,2,3}
//If string array
string [] strArr={“Amit”,”Amit”,”Kumar”,”Kumar”};
strArr = strArr.Distinct().ToArray(); //Output: strArr ={“Amit”,,”Kumar” }
string [] strArrCase={“Amit”,”AMIT”,”Kumar”,”KUMAR”,”Amit”,”Kumar”};
strArrCase = strArrCase.Distinct().ToArray(); //Output: strArrCase ={“Amit”,”AMIT”,”Kumar”,”KUMAR” }
Custom Function:
//Function call
string [] strArr={“Amit”,”Amit”,”Kumar”,”Kumar”};
strArr= GetUniqueArray(strArr); //Output: strArr ={“Amit”,”Kumar” }
        #region Get unique From Array
        public static string[] GetUniqueArray(string[] itemValues)
        {
            string stringList=string.Empty;
            string currentValue=string.Empty;
            string[] newArray=null;
            ArrayList uniqueList = new ArrayList();
            try
            {
                for (int counter = 0; counter < itemValues.Length; counter++)
                {
                    currentValue = Convert.ToString(itemValues[counter]);
                    if (!CheckDuplicateArray(currentValue, uniqueList))
                    {
                       uniqueList.Add(itemValues[counter]);
                    }
                }
                if (uniqueList.Count != 0)
                {
                    newArray = uniqueList.ToArray(Type.GetType("System.String")) as string[]; 
                }
            }
            catch (Exception ex)
            {
            }
            return newArray;
        }
        #endregion

        #region Check Duplicates From Array
        public static bool CheckDuplicateArray(string value, ArrayList uniqueList)
        {
            bool itemFound = false;
            try
            {
                foreach (string currentValue in uniqueList)
                {
                    if (string.Equals(value, currentValue, StringComparison.InvariantCultureIgnoreCase))
                    {
                        itemFound = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return itemFound;
        }
        #endregion

Comments

Popular posts from this blog

Sitecore GraphQL Queries

Sitecore Experience Manager Cloud (XM Cloud) Building blocks

Configuring Sitecore Next.js Headless SXA Multisite App in a Sitecore Container