How to Replace Nested foreach Loops With a LINQ ExpressionThe following nested foreach loops search for all test methods in an assembly:
IEnumerable<String> tests = new List<String>();Using LINQ this function can be rewritten in a very compact yet very readable form:
Assembly a = Assembly.LoadFrom(dll);
Type types = a.GetTypes();
foreach (Type type in types)
if (type.IsDefined(typeof (TestClassAttribute), true))
MethodInfo methods = type.GetMethods();
foreach (MethodInfo method in methods)
if (method.IsDefined(typeof (TestMethodAttribute), true))
return from type in Assembly.LoadFrom(dll).GetTypes()The first from - where part selects all types which are test classes. The second from where part walks through this list and searches for all methods which are test methods. From these methods the method name is added to the return value.
where type.IsDefined(typeof (TestClassAttribute), true)
from method in type.GetMethods()
where method.IsDefined(typeof (TestMethodAttribute), true)
Note that in LINQ you can combine from - where clauses to generate nested queries.