You're starting with false
and then adding and
clauses.
false && ...
always returns false
, so your Where
clause will never match anything.
Try starting with:
var predicate = PredicateBuilder.True<DataRow>();
ALSO:
You are closing over a loop variable, which means all your predicates will use the last parm
in parms
.
You can fix this by creating a local copy in your loop:
foreach( var parm in parms )
{
if (dataColumnCollection.Contains(parm.Key))
{
var copy = parm;
predicate = predicate.And( p => p[ copy.Key ] == copy.Value );
}
UPDATE:
DataRow[ key ]
is of type object
, so in p[ copy.Key ] == copy.Value
, the equality operator is object reference equality, not string equality.
You can fix this by specifying String.Equals
:
predicate = predicate.And( p => String.Equals( p[ copy.Key ], copy.Value ) );
Interestingly, this example shows that you can have multiple instances of string
with the same contents.