Using Multiple Where Clauses in a LINQ Query

By Jay at December 06, 2008 15:32
Filed Under: .NET
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 :


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 };


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 !

Comments

12/7/2008 7:43:54 PM #

Pingback from blogs.codes-sources.com

Utiliser plusieurs clauses Where dans une requête LINQ , Jerome Laban

blogs.codes-sources.com

3/29/2010 3:38:30 AM #

using many where conditions for example

return (from p in jobsMSdc.GetTable<ColumnsSetup>()
        where p.TableName == tableName.ToString()
        where p.isDisplay ==true
        orderby p.ordering
        select p).ToList<ColumnsSetup>();

here u can c got 2 line of where condition to filter 2 type of filtering.

Yer Kuan Chern Malaysia

5/20/2010 11:11:27 AM #

Pingback from 219.tgrconversions.com

Pcr2500 Suburban Intake, R2500 Slot

219.tgrconversions.com

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




About me

My name is Jerome Laban, I am a Software developer and .NET enthustiast from Montréal, QC. You will find my blog on this site, where I'm adding my thoughts on current events, or the things I'm working on, such as the Bluetooth Remote Control Software for Windows Mobile.