views:

97

answers:

2

I have a Dictionary<string, List<string>>

I want to do a check that all Keys in the dictionary have at least 1 item in its corresponding list

+8  A: 

You can use the Enumerable.All extension method (part of the LINQ extension methods) for this.

bool allPopulated = yourDictionary.All(p => p.Value != null && p.Value.Count > 0);
Adam Robinson
All's well that ends well, eh? ;)
Dan Tao
@Dan: Haha, indeed ;)
Adam Robinson
+14  A: 

Try the following

bool allPopulated = map.All(p => p.Value != null && p.Value.Count > 0);
JaredPar
Odd, beat you slightly yet you get more votes.
Adam Robinson
+1 for elegance.
CesarGon
@Adam: I'm pretty sure your answer didn't have the null check at first.
Dan Tao
I voted for this one, since he shortened it far enough that there is no scroll bar (which is quite ugly.)
mquander
@Dan: While it did, that isn't even an explicit requirement for the OP's question.
Adam Robinson
@Adam, I've been on both ends of that type of event. There was a slight difference though in that your original answer didn't have the `null` check. Typically I'd consider it completely unnecessary as it's bad practice to have null values IMHO but OP wasn't specific on the issue so I added it.
JaredPar
@Jared: I agree that it's unnecessary, but my answer did include the null check.
Adam Robinson
Poor `.Any()` is so often forgotten and somewhat more elegant.
Marc
@Adam: I thought it didn't -- if you say it did, I guess I'm mistaken. (But if that's the case then my eyes deceived me because I honestly remember looking at your answer and Jared's and *believing* I noticed that as a difference.) In any case I was only suggesting that could've been a reason this answer got more upvotes, nothing more. Of course the obvious alternative reason is that Jared's got 102k rep. But then, you've got 30k, and this was a pretty straightforward question; so it seems kind of silly (to me) even to be having this conversation.
Dan Tao
@Dan: You're right, it's really not worth discussing; there's enough of a dustup on Meta about the random sorting that equivalently-voted answers receive without worrying about it here. @Marc: Out of curiosity, how is `Any` more elegant? I'm pretty sure they would both short-circuit at the same point, and `All` expresses the OP's literal question; `Any` would be functionally identical, but requires that you flip the question around ("are any empty" instead of "are all populated").
Adam Robinson
@Marc: I'd have to second Adam on this one. To me using `Any` for this problem would be equivalent to something like `if (!IsNotReady) { ... }` (negative conditions have always struck me as a bit cumbersome).
Dan Tao
@Dan, Adam: ahh, the light turns on and I understand you're confusion. I should've just elaborated `.Any` in place of `.Count > 0`. Sorry for the confusion.
Marc
@Marc: Yes, I'm pretty sure that's where the confusion crept in (I know it was for me, anyway). We thought you were suggesting `!Any` instead of `All`. That said, I would generally opt for `Count > 0` or `Length > 0` for collections where you *know* the `Count`/`Length` property is simply a cached value (as is the case for `List<T>`). This will actually perform better than `Any`, which needs to enumerate over one item (making it effectively `GetEnumerator().MoveNext()` -- two method calls). Of course that's a pretty insignificant issue.
Dan Tao