How to parameterize a boolean expression in Linq?

I have a Linq statement that looks something like:

Report.Where(a=>a.Property == 1 || a.Property == 2 || a.Property == 3).Count()

I would like to know if I can refactor the statement into something of the form:

Report.Where(a=a.Property == validProperties)

where I can store a set of validProperties as an array of integers.

Answers


You can use Contains method

var idsToCheck = new List<int> {1,2,3};

var someCount = Report.Where(a=>idsToCheck.Contains(a.Property)).Count();

Or call the Count first and pass the lambda expression to that.

var someCount = Report.Count(a => idsToCheck.Contains(a.Property));

Need Your Help

Irrlicht LNK2019: unresolved external symbol __imp__createDevice referenced in function _main

c++ visual-studio-2010 irrlicht

I am trying to build the example 01.HelloWorld" of Irrlicht using VS 2010. When I do I get the error:

Running PHP - when using a node instance and port

php ajax linux node.js

I'm running an application using nodejs and it's listening on port 8000, so my web url bar is ip:8000. I have an ajax call to a php file: