Just for fun, I made a little quiz following my previous LINQ article.
Question 1
What is the difference in effect of the following code sections? Note that “Failed” is a nullable bit, so we will label the states as red, green and yellow (null).
// Option A
where (filter.Failed.HasValue && doc.Failed.HasValue ? filter.Failed == doc.Failed : true)
// && (...) -- more filters here
// Option B
where (filter.Failed.HasValue ? filter.Failed == doc.Failed : true)
// && (...) -- more filters here
Question 2
What would happen if all the queries ended with “false”?
where (... ? ... : false) && (... ? ... : false)
Question 3
Keeping both performance and readability in mind, what is the difference between grouping all the queries together (see option A), against having each in a separate section (see option B)?
// Option A
docs = from doc in docs
where (filter.Failed.HasValue ? filter.Failed == doc.Failed : true)
// && (...) -- more filters here
select doc;
// Option B
if (filter.Failed.HasValue)
docs = from doc in docs
where filter.Failed == doc.Failed
select doc;
// more filter blocks here
Jonas is your typical manual labour, but he specializes in painting. Not only does he enjoy it, he’s also rather good at it. One day Jonas gets a job at a house in need of painting, however, he is asked to cut the grass instead. Every month when Jonas asks about painting the walls he is told that the grass takes priority, and every month the grass grows back again.