Click here to Skip to main content
15,885,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there,
I have a dataview which is filled by an object using string lists. I want pick the suitable entries by typing search words in a textbox.

my object:
C#
class my_object
{
    List<string> column1 = new List<string>();
    List<string> column2 = new List<string>();
    List<string> column3 = new List<string>();
    List<string> column4 = new List<string>(); 
}


my entries for the dataview:
C#
List<my_object> entries = new List<my_object>();



My aim is to filter the entries like the search function in the windows explorer but with the difference that i want to include four columns an not just the column with the filename.
Is there any possibility to do this?

Hope you can help me.

What I have tried:

C#
internal static List<my_object> SearchObject(this List<my_object> Source, List<string> SearchWords)
{
    List<my_object> results = new List<my_object>();

    foreach (my_object m in Source)
    {
        foreach(string s in SearchWords)
        {
            // Filter Column 1
            foreach(string c1 in m.column1)
            {
                if(c1.IndexOf(s) != -1)
                {
                    results.Add(m);
                    break;
                }
            }
        } 
    }

    return results;

    // Problem:
    // This function only filters the first column.
    // If I want to filter the next column, I have to break all 'foreach' blocks 
    // except the '(my_object m in Source)' block...

    // It the 'break' would work for more the one loop, this method would work...
}
Posted
Updated 25-Dec-16 9:00am
Comments
Tomas Takac 25-Dec-16 14:58pm    
You should not have an object with lists, rather you should have an class with 4 properties and then have a list of that.

1 solution

Hi, I think result List should contain matches from each column?
If you're using C#, probably should have no problems using Linq
C#
//Source in this case refers to each column
//SearchWords list of strings to search
public List<string> SearchList(List<string> Source, List<string> SearchWords){
//your code can be shortened to Linq query similar to the following
List<string> results = Source.Where(x => SearchWords.Contains(x)).ToList(); 
//in sql this would translate similiar to:
//select Source.Value 
//from Source
//where Source.Value in ('Value1', 'Value2') --SearchWords
return results;
}

Regarding SearchObject, what is the use of passing List<my_object> Source when it seems you need only 1 my_object?
You can use the Linq query for the 4 lists in my_object.

my_object data = new my_objec(); //lists containing all data

List<my_object> entries = new List<my_object>();

So then
entries.column1 = SearchList(data.column1, SearchWords); //similar for the rest
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900