Filtering Generic C# List

Mostly while filtering List, you can simply use Linq Where or string.Contains method, e.g., if you have astring "My name is Khan" and you want to compare that with “My name” or “is Khan” so string.Containswould be the simplest way to go through but what if you require to compare each word regardless of their sequence like if you have to compare “My name is Khan” with “Khan name” in this case string.Contains would returnfalse.

public static bool SqlLike(string strMain, string strSub)
        {
            string[] subStrings = strSub.Split(' ');

            foreach (string subString in subStrings)
            {
                if (strMain.Contains(subString) == false)
                {
                    return false;
                }
            }
            return true;
        } 


private bool FilterBarFun(Item value)
        {
            return SqlLike(value.ItemName.ToUpper(),txtFilterString.Text. ToUpper());                 
        }


private void txtFilterString_TextChanged(object sender, EventArgs e)
        {
            subItems = items.FindAll(FilterBarFun);

            if (txtFilterString.Text.Length > 0)
            {
                source.DataSource = subItems;
            }
            else
            {
                source.DataSource = items;
            }
        }
Advertisements
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s