-2

Here is the values I am adding in Dictionary List:-

1,1000
2,2000
1,1000
3,3000

I can delete from Dictionary based on key value:-

if (i == null)
    return;
var dict = app["ItemList"] as Dictionary<int, int>;
if (dict != null)
    dict.Remove(i.ItemId);

Here I pass key value (i.ItemId),it deletes an item from dictionary. But When I use List of Dictionary, I couldn't delete items from List of Dictionary based on Key Value.

if (i == null)
    return;
var dict = app["ItemList"] as List<Dictionary<int, int>>;
if (dict != null)
{
    var itemDict = new Dictionary<int, int>();
    itemDict[i.ItemUid] = 0;
    dict.Remove(itemDict);
}

Note:- I have only Key value available when I am deleting from List of Dictionary.

Help will be appreciated.

user1327064
  • 4,187
  • 5
  • 20
  • 30
  • can u refer this link : https://stackoverflow.com/questions/1636885/remove-item-in-dictionary-based-on-value – Ali Feb 17 '18 at 13:49
  • No.. delete a record from List-Dictionary-int,int--. I understood how to delete from Dictionary-int,int- – user1327064 Feb 17 '18 at 13:54
  • You need to identify and retrieve the dictionary from the list first. How do you intend to distinguish the dictionaries inside the list? – Mong Zhu Feb 17 '18 at 13:57
  • @MongZhu. When I have to delete, I have only Key value available. I need to delete all records where Key value is matching. – user1327064 Feb 17 '18 at 14:00
  • Does the `ItemUid` exist only in one of the dictionaries inside your list? Or can it occur in multiple dictionaries? If it is unique then you can check for it – Mong Zhu Feb 17 '18 at 14:01
  • itemUid exists in multiple dictionaries... That is the reason I have used List of Dictionary instead of just Dictionary.. Coz keys can be duplicate. – user1327064 Feb 17 '18 at 14:02
  • Just go over all dictionaries in a list with foreach and delete item from each one. – Evk Feb 17 '18 at 14:02
  • Then I would suggest that you loop through the list and delete all records in each dictionary like you did in the first example – Mong Zhu Feb 17 '18 at 14:02
  • Or do you aim explicitly for a linq solution? – Mong Zhu Feb 17 '18 at 14:09
  • Dictionary does not implement equality like that. Two identical Dictionary are not Equal. Question is not clear. VTC – paparazzo Feb 17 '18 at 14:14

1 Answers1

1

Assuming that i.ItemUid is a key:

var dict = app["ItemList"] as List<Dictionary<int, int>>;
if (dict != null)
{
    foreach (var x in dict) x.Remove(i.ItemUid);
}

If instead i.ItemUid is a key-value pair

var dict = app["ItemList"] as List<Dictionary<int, int>>;
if (dict != null)
{
    foreach (var x in dict) x.Remove(i.ItemUid.Key);
}

Edit: total number of keys in all dictionaries:

int totalNumberOfKeys = 0;
foreach (var x in dict) totalNumberOfKeys += x.Count;
oliver
  • 2,771
  • 15
  • 32
  • ItemUid is a key... And first example is kind of working. It deleted matched records but dict.Count is still same. How do i get rid of empty items, if you can help. THanks – user1327064 Feb 17 '18 at 14:11
  • 1
    @user1327064 why do you expect the count of `dict` to change? For that you would have to remove an entire dictionary from the list – Mong Zhu Feb 17 '18 at 14:22
  • @user1327064: probably you are thinking that dict.Count is the total number of keys... it is not! If you want that number, see the following edit to my answer. – oliver Feb 17 '18 at 14:26