Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass predicate to function in C#?

I have :

 public void InitializeStatusList(DropDownList list)
    {
       var dictionaryEntries = GetEntriesFromDatabase();
       list.DataSource = dictionaryEntries.Where(entry => entry is EntryStatus1 || entry is EntryStatus2);
       list.DataBind();           
    }

I have many of these functions. I want to write common function with dictionaryEntries query condition passed as predicate.

For example:

public void InitializeStatusList(DropDownList list)
{
    CommonInitializeStatusList(DropDownList list, entry => entry is EntryStatus1 || entry is EntryStatus2);
}

public void CommonInitializeStatusList(DropDownList list, ??????????????? predicate)
{                       
    var dictionaryEntries = GetEntriesFromDatabase();
    list.DataSource = dictionaryEntries.Where(predicate);
    list.DataBind();        
}

What stands for ???????????????

Thanks in advance

like image 825
Michał Kuliński Avatar asked May 10 '12 08:05

Michał Kuliński


People also ask

How do you pass a predicate to a method in C#?

A predicate delegate is defined; it takes the IsPositive method as parameter. var filtered = data. FindAll(predicate); We pass the predicate to the FindAll method of a list, which retrieves all values for which the predicate returns true.

What is a predicate function in C?

Predicate functions are functions that return a single TRUE or FALSE . You use predicate functions to check if your input meets some condition. For example, is. character() is a predicate function that returns TRUE if its input is of type character and FALSE otherwise.

What is a predicate code?

Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value.


1 Answers

Func<Entry, bool> predicate should work, where Entry is type of entry variable.

like image 50
Marek Dzikiewicz Avatar answered Sep 23 '22 21:09

Marek Dzikiewicz