Array to Dictionary

I am trying to get this one _valueAdds = List<ValueAddedItemHelper>in gridItems( Dictionary) with _valueAddsboth key and all values false. But I'm not sure how to do this with Lamda. This is how far I fell below. I did it with a while loop, but I would like to learn how to do it with Lamda

gridItems = new Dictionary<ValueAddedItemHelper, bool>();
gridItems = _valueAdds.Select(k => new { k }).ToArray().ToDictionary(t => t, false);
+3
source share
6 answers
_valueAdds.ToDictionary(t => t, t => false);
+5
source

You need to provide the lambda expression as the second argument (or create the delegate in some other way, but the lambda expression will be easier). Note that a call is ToArraynot required, and not a single empty dictionary that you create to begin with. Just use:

gridItems = _valueAdds.Select(k => new { k })
                      .ToDictionary(t => t, t => false);

, , ... , ValueAddedItemHelper. ? , :

gridItems = _valueAdds.ToDictionary(t => t, t => false);
+1

You do not need ToArray (). ToDictionary (). You can just do ToDictionary (). And you do not need the first line. The second line will create and populate the dictionary.

The code:

gridItems = _valueAdds.ToDictionary(p => p, p => false);
0
source

Assuming that _valueAddsis IEnumerable<ValueAddedItemHelper>, you can do this:

gridItems = _valueAdds.ToDictionary(x => x, x => false);
0
source

Sort of

var gridItems = _valueAdds.ToDictionary(k=>k,i=>false);
0
source

Problem Select(k => new { k }); which creates an anonymous type with a property k. Simply:

var gridItems = _valueAdds.ToDictionary(t => t, t => false);
0
source

All Articles