-1

I have an Object array of my class (Item) like below,

class Item
{
    int id;
    int value;
    bool isSelected;
}

Item itemArr[] = new Item [MAXCOUNT];   

To retrieve the selected Item ids and store into list, I am using below code

List<int> listOfItems = new List<int>();
listOfItems.AddRange(itemArr.Where(p => (p.IsSelected)
                            .Select(p => p.ID)).ToArray());

I have one more Dictionary which indicates item id as key and item value as value,

Dictionary<int,int> itemidValueMap = new Dictionary<int,int>();

How to get item id and value pair and store it into this dictionary?

This question is not duplicate. I need to select only the elements which satisfies the condition and store only them in dictionary. But previous question says about adding all the elements into dictionary.

srajeshnkl
  • 883
  • 3
  • 16
  • 49
  • Your code wonßt even compile, as `Item.IsSeleceted` has no member `Select`. You probably missed a bracket. I supoose you mean `istOfItems.AddRange(itemArr.Where(p => p.IsSelected).Select(p => p.ID).ToArray());`. – MakePeaceGreatAgain Aug 10 '17 at 09:50
  • You can't add a range of items of the type if `ID` as they are int and not `Item`. Please add compiling code – Gilad Green Aug 10 '17 at 09:51
  • You probably want to add those items into your list that are selected - not just their ids. Omit the `Select` from your code. – MakePeaceGreatAgain Aug 10 '17 at 09:52
  • @HimBromBeere I have edited my code now to List from List. Hope this compiles. – srajeshnkl Aug 10 '17 at 09:55
  • @MongZhu : How to convert array to dictionary explains about getting all the array elements into dictionary. I need solution to select only the values which satisfies the condition and store them into dictionary. – srajeshnkl Aug 10 '17 at 10:05

3 Answers3

2

To construct the dictionary use ToDictionary:

var dictionary = itemArr.Where(p => p.IsSelected)
                        .ToDictionary(key => key.id, value => value.value);

If you have the same id a few times it will cause an exception (keys are not unique) so use ToLookup or first GroupBy and then ToDictionary.

var lookup = itemArr.Where(p => p.IsSelected)
                    .ToLookup(key => key.id, value => value.value);

var dictionary = itemArr.Where(p => p.IsSelected)
                        .GroupBy(i => i.id)
                        .ToDictionary(key => key.Key, value => value.Select(i => i.value));

As a side note you can populate the ID list a bit nicer:

var listOfItems = itemArr.Where(p => p.IsSelected).Select(p => p.ID).ToList();
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Your code selects all the itemid and value into dictionary. I need to select the items which has IsSelected value as true when adding it into dictionary. How to do ? – srajeshnkl Aug 10 '17 at 10:01
  • @RajeshSubramanian - Did this help you understand how to do so? – Gilad Green Aug 10 '17 at 10:16
2

To get the selected IDs your query should look like this:

List<int> selectedIDs = itemArr.Where(item => item.IsSelected).Select(item => item.id).ToList();

Note that the result is a List<int>, not a List<Item>.

To get your dictionary try this:

var idToValue = itemArr.ToDictionary(item => item.id, item => item.value);

This only works if your IDs are unique. If they are not, you'll need to group them, but I don't know how you want to aggregate the values of items with the same ID.


If you want that dictionary only for selected items, you simply insert the Where clause again:

var idToValue = itemArr.Where(item => item.IsSelected).ToDictionary(item => item.id, item => item.value);
René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

You need to change this

listOfItems.AddRange(itemArr.Where
                  (p => (p.IsSelected).Select(p => p.ID)).ToArray());

to

listOfItems.AddRange(itemArr.Where
                  (p => p.IsSelected).ToArray());

then

Dictionary<int,int> itemidValueMap = listOfItems.ToDictionary(item => item.id,
                                   item => item.value)
npo
  • 1,060
  • 8
  • 9