Questions tagged [predicate]

A Predicate is a method which represents a set of criteria and decides for a given object if these criteria are fulfilled or not. In computer languages a Predicate is expressed as a function which takes a single object as input parameter and returns a boolean value.

A Predicate is a method which represents a set of criteria and decides for a given object if these criteria are fulfilled or not. In computer languages a Predicate is expressed as a function which takes a single object as input parameter and returns a boolean value.

Example of a Predicate in .NET Framework / C#

In the .NET Framework the most general form of a Predicate is represented by a generic delegate:

public delegate bool Predicate<in T>(T obj)

An often used alternative representation is:

public delegate TResult Func<in T, out TResult>(T arg)

where TResult is of type bool.

An example of a concrete instance of such a delegate is a method which decides for a given Product by a special citerion if it is expensive or not:

public bool IsExpensive(Product product)
{
    return product.UnitPrice > 1000;
}

Predicates are often used for defining criteria to filter a subset of data out of a larger set and are often expressed as anonymous methods or by lambda expressions:

IEnumerable<Product> expensiveProducts =
    allProducts.Where(p => p.UnitPrice > 1000);
1908 questions
37
votes
2 answers

Howto use predicates in LINQ to Entities for Entity Framework objects

I'm using LINQ to Entities for Entity Framework objects in my Data Access Layer. My goal is to filter as much as I can from the database, without applying filtering logic to in-memory results. For that purpose Business Logic Layer passes a…
bairog
  • 3,143
  • 6
  • 36
  • 54
36
votes
3 answers

IEnumerable.Contains with predicate

I need just to clarify that given collection contains an element. I can do that via collection.Count(foo => foo.Bar == "Bar") > 0) but it will do the unnecessary job - iterate the whole collection while I need to stop on the first occurrence. But I…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
33
votes
4 answers

How can I negate a lambda Predicate?

Lets say I have a Stream of Strings. final Stream stream = ...; I want to filter out each empty string after trimmed. stream .filter(Objects::nonNull) .map(String::trim) .filter(v -> !v.isEmpty()); Is there any way to apply…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
32
votes
7 answers

What is the difference between a Predicate and a Function Interface in Java8?

I know it may be a very basic question on SO but I want to know what is the difference between a Predicate and a Function Interface in Java8? Predicate predicateTest = (s)-> s.length() > 5; …
NullPointer
  • 7,094
  • 5
  • 27
  • 41
30
votes
2 answers

Is there any way to negate a Predicate?

I want to do something like this: List list1 = ... List list2 = ... Predicate condition = ... ... list2.RemoveAll (!condition); ... list2.AddRange (list1.FindAll (condition)); However, this results in a compiler…
Matthew
  • 28,056
  • 26
  • 104
  • 170
29
votes
5 answers

How can pointers be totally ordered?

Pointers in C++ may in general only be compared for equality. By contrast, less-than comparison is only allowed for two pointers that point to subobjects of the same complete object (e.g. array elements). So given T * p, * q, it is illegal in…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
26
votes
3 answers

Is there a way to drain parts of a vector based on a predicate?

I'm trying to remove some elements from a vector, based on a predicate, and collecting the result. Here's a (not working) example with an expected result: let mut v: Vec = vec![1, 2, 3, 4, 5, 6]; let drained: Vec = v.iter().filter(|e|…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
26
votes
4 answers

Predicates vs if statements

I have seen in some projects that people use Predicates instead of pure if statements, as illustrated with a simple example below: int i = 5; // Option 1 if (i == 5) { // Do something System.out.println("if statement"); …
poyger
  • 636
  • 2
  • 8
  • 17
25
votes
5 answers

Correct Way to Define a Predicate Function in C++

I'm trying to write predicate function for use with STL algorithms. I see that they are two ways to define a predicate: (1) Use a simple function as below: bool isEven(unsigned int i) { return (i % 2 == 0); } std::find_if(itBegin, itEnd, isEven);…
cppcoder
  • 1,194
  • 4
  • 16
  • 30
25
votes
4 answers

What is Predicate Dispatch

I have seen much talk about predicate dispatch in Clojure lately and wonder if there is something to this thing. In other words, what is predicate dispatch and how does it differ from generic functions, OOP polymorphism, and patterns? Thank you
Eli Schneider
  • 4,903
  • 3
  • 28
  • 50
25
votes
3 answers

Default value on generic predicate as argument

First time question for me :) I need some way to define a default predicate using a generic on the format Func and then use this as a default argument. Something like this: public bool Broadcast(byte command, MemoryStream data, bool…
Bakery
  • 406
  • 1
  • 5
  • 10
25
votes
6 answers

How do I form a good predicate delegate to Find() something in my List?

After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class) For example: public class Car { public string Make; public string Model; …
Pretzel
  • 8,141
  • 16
  • 59
  • 84
24
votes
1 answer

MOQ - LINQ Predicates in Setup Method

In my method, I have my repository doing this: bool isConditionMet = MyRepository.Any(x => x.Condition == true); I am attempting to mock this using MOQ like so: MyMockedRepository.Setup(x => x.Any(y => y.Condition == true)).Returns(true); However,…
Brandon
  • 10,744
  • 18
  • 64
  • 97
23
votes
3 answers

The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework

can anyone help me out in solving my issue. I am using the code given below: public IEnumerable Getdata(Expression> predicate) { return…
zaheer abbas
  • 353
  • 1
  • 2
  • 15
21
votes
4 answers

Isn't Func and Predicate the same thing after compilation?

Haven't fired up reflector to look at the difference but would one expect to see the exact same compiled code when comparing Func vs. Predicate I would imagine there is no difference as both take a generic parameter and return bool?
Sean Chambers
  • 8,572
  • 7
  • 41
  • 55