1

I'm looking for some advice on a good way to achieve my goal. I feel like my pseudo code below is redundant and that there must be a more efficient way of doing it. My question is there a better solutioj to this or more efficcient way? So here is the setup...

I have a class called Node which has two properties

class Node
{
    bool favorite
    string name
}

I have a list which contains around a thousand of these Nodes. I want to give users three features..

  1. A way to filter the list to just show favorites otherwise if favorites is false it displays the original list

  2. Ability to search by string/name comparison

  3. The ability for both the search and favorite to work in combination

enter image description here

below is my pseudo code - describes an approach, not ideal though. You can read the comments in the code to get the main gist.

// initial collection of nodes
list<Nodes> initialnodesList = [];
// list of nodes which are displayed in UI
list<Nodes> displayNodes = [];

public void FilterNodes()
{
    list<Nodes> tempNodesList = [];

    if (favoritesEnabled)
    {
        // collect favorites
        foreach (n in initialnodesList)
            if (n.favorite)
                tempNodesList.add(n);

        // search within favorites if needed and create new list
        list<Nodes> searchedNodesList = [];
        if (!isStringNullWhiteSpace(searchString))
        {
            foreach (n in tempNodesList)
                if (n.name == searchString)
                    searchedNodesList.add(n);

            displayNodes = searchedNodesList;
            return;
        }else{
            return;
        }
    }
    else
    {
        // search within initial node collection if needed and create new list
        list<Nodes> searchedNodesList = [];
        if (!isStringNullWhiteSpace(searchString))
        {
            foreach (n in initialnodesList)
                if (n.name == searchString)
                    searchedNodesList.add(n);

            displayNodes = searchedNodesList;
            return;
        }

        // if search is not needed and favorites were not enabled then just return the original node collection
        displayNodes = initialnodesList;
        return;
    }

}
1
  • 1
    What exactly is your goal here? Do you want to code it in C# and make the code look cleaner? If so, you should have a look at Linq, especially IEnumerable.Where Commented Oct 31, 2015 at 7:33

1 Answer 1

3

You can optimize your code with linq statement to filter based on searchString and favorite option.

public List<Node> FilterNodes(bool seachFavorite, string searchString)
{
     return initialnodesList.Where(l => (string.IsNullOrEmpty(searchString) || l.name.StartWith(searchString, StringComparison.OrdinalIgnoreCase)) && l.favorite == seachFavorite).ToList();
}

Also, optimize your code to look for search with StartWith, you can changed to Contains if you want search has to be done based on contains string search.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't this always be searching for the favorite and string. In some cases you may only be using one or the other.
Updated the linq statement so it will search in the list of the search string is not null or empty and favorite it is passed as true. So, if you have to look only for favorite then pass searchString as Null and searchFavorite as true in FitlerNodes method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.