It's a piece of cool programming from a piece of software I wrote this year to create random maths questions (with answers).
It's cool mostly because it's one line of programming (though it's split over several lines for readability).
The main program never changes (as it just creates maths questions) but the repositories from which it chooses the questions must be capable of being added to without having to re-compile the main program, so they are placed in separate files called assemblies.
The function examines the assemblies that contain the maths questions and creates (and returns) a list of all available maths questions that the user can choose from.
return assemblies
.SelectMany( turn a list of lists into a single list
x => x
.GetTypes() get all the types in the assemblies
.Where(t => t.Namespace != null && t.Namespace.Contains(".LCQ") && t.Name.StartsWith("LCQ")) filter out the ones we need
.SelectMany( turn the inner list of lists into a single list
u => Enumerable
.Range(0, (int)u.GetProperty("Count").GetValue(null)) get the number of questions in each type and number them
.Select( for each question, select the question title, the question number, and the question type
v => (
Title: (string)u.GetMethod("Title").Invoke(null, new object[] { v }),
QNo: v,
QType: u
)
)
.ToList()
)
)
.ToDictionary( create a dictionary out of the questions indexed by their title
t => t.Title,
t => (t.QType, t.QNo)
);
Ten or fifteen years ago, this would have been at least a hundred lines of code. With LINQ, it's reduced to one line of code.
And Roger has the nerve to call it 'average'. Philistine!