tags:

views:

249

answers:

5

I have a LINQ query which returns all the values inside a Dictionary, conditional upon something:

var apps =
from entry in shape.Decorators
where entry.Value == DecoratorLayoutStyle.App
select entry.Key;

shape.Decorators is a

Dictionary<Shape, DecoratorLayoutStyle>

Is there something terser, and/or can I use a combination of lambdas or something?

+1  A: 
var apps = Shape.Decorators.Where(x => x.Value == DecoratorLayoutStyle.App)
                           .Select(x => x.Key);
Mehrdad Afshari
+1  A: 
var apps = shape.Decorators
                     .Where(e => e.Value ==  DecoratorLayoutStyle.App)
                     .Select(e => e.Key);

Do you think this is terser?

Personally I prefer the query syntax when I have more than one LINQ operator all the operators I use can be translated to it.

Martinho Fernandes
+3  A: 

That looks plenty terse to me, I guess you could use the extension functions instead of the from/select linq syntax, but that wouldn't be too different.

More imporantly, I'm not sure you want terser. The current format is very readable, and clearly documents exactly what you're trying to do.

brien
i agree, thanks
geejay
+5  A: 
var apps = shape.Decorators
                .Where(x=>x.Value == DecoratorLayoutStyle.App)
                .Select(x=>x.Key);

I think yours is just as fine.

Will
A: 

Just to be different.

var apps = shape.Decorators.Keys
  .Where(k => shape.Decorators[k] == DecoratorLayoutStyle.App);
David B