Skip to content

Removing a selected entry by key in IDictionary in NHibernate

October 22, 2012

Researching the need for finding a way to remove a selected entry by the key of the Dictionary object in NHibernate, I initially thought by specifiying the index would remove the IDictionary key and value. No, you have to loop through the IDictionary like so to remove your selected entry you wish to remove.

Here’s how, using the customer and bank example: –

    protected void DeleteItem(object sender, DataGridCommandEventArgs e)
        if (e.CommandName == “delete”)
            DataGridItem container = (DataGridItem)e.Item;
            string customerName = container.Cells[1].Text;

            // imagine customer ID was in grid column 1, customer name was in grid column 2, sort code ID was in grid column 3, and bank account name was in grid column 4.

            short sortCodeID = Convert.ToInt16(container.Cells[2].Text.ToString());

            Customer = Customer.GetCustomerByName(customerName);

            if (customer != null)

                IDictionary<short, string> dictionary = customer.Bank;
                //KeyValuePair<short, string> k = dictionary.ElementAt(0);

                foreach (var item in dictionary.Where(kvp => kvp.Key == vlanID).Take(1).ToList())




As you can see you update the whole Customer object, and the dictionary object containing the key sortCodeID would be removed through the loop.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: