tags:

views:

84

answers:

3

I have a Dictionary

private readonly Dictionary<int, BinaryAssetExtensionDto> _identityMap;

And I would like to do something like this:

if(_identityMap.Values.Contains(x => x.extension == extension))...

Is this possible because previous code doesn't work.

Now I'm doing it like this:

var result = _identityMap.Values.ToList().Find(x => x.extension == extension);
if (result != null) return result;
+2  A: 
return _identityMap.Values.FirstOrDefault(x => x.extension == extension);

This may return null if the condition is not satisfied. If this is not what you want you may provide a default value:

return _identityMap.Values.FirstOrDefault(x => x.extension == extension) ?? 
    new BinaryAssetExtensionDto();
Darin Dimitrov
+5  A: 
using System.Linq;
...
_identityMap.Values.Any(x=>x.extension==extension)
Arnis L.
Does it return null if none are found?
Lieven Cardoen
@Lieven it returns boolean if any element in collection meets predicate.
Arnis L.
Oh, great, just what I need.
Lieven Cardoen
It's nice to help. :)
Arnis L.
+1  A: 

I believe that either of the following would work for you:

if (_identityMap.Values.Where(x => x.extension == extension).Count() > 0) { /*...*/ }

if (_identityMap.Values.FirstOrDefault(x => x.extension == extension) != null)  { /*...*/ }

There are probably other possible alternatives too

Steve Greatrex