Using Multiple Where Clauses in a LINQ Query
Cet article est disponible en francais.
After writing my previous article where I needed to intercept exceptions in a LINQ Query, I found out that it is possible to specify multiple where clauses in a LINQ Query.
Here is the query :
[code:c#]
var q = from file in Directory.GetFiles(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727", "*.dll")
let asm = file.TryWith(f => Assembly.LoadFile(f))
where asm != null
let types = asm.TryWith(a => a.GetTypes(), (Exception e) => new Type[0])
where types.Any()
select new { asm, types };
[/code]
This query is able to find assemblies for which it is possible to list types. The point of using multiple Where clauses is to avoir evaluating chunks of a query if previous chunks can prevent it. By the way, the TryWith around the Assembly.GetTypes() is there to intercept exceptions raised when loading types, in case dependencies would not be available at the moment of the enumeration.
A useful LINQ trick to remember !