简体   繁体   中英

How to remove any value from Dictionary<string,List<string>> using LINQ

I have a dictionary Dictionary<string,List<string>> I want to remove any item from List using Linq statement. Here is my code

 Dictionary<string, List<string>> dic1 = new Dictionary<string, List<string>>();
 dic1.Add("K1", new List<string>() { "ss", "ss1" }); 
 dic1.Add("K2", new List<string>() { "ss2", "ss3" });

I want to remove item 'ss' from this dictionary which is present on any key. The following code working fine for me.

foreach (KeyValuePair<string,List<string>> kvp in dic1)
{
    if (kvp.Value.Contains("ss"))
    {
        kvp.Value.Remove("ss");
    }
}

Is there any possibilities to use Linq statement here?? Thanks....

List.Remove doesn't fail when the item doesn't exist (it returns a bool telling whether it succeeded) so there's no reason to check beforehand. Simply remove it. Also, since you don't care about the keys, you can iterate over just the values (lists in your case):

foreach (var list in dic1.Values)
{
    list.Remove("ss");
}

There's no reason to use LINQ here since LINQ is for querying data and you don't have a query. You just need to remove the items.

 foreach (KeyValuePair<string, List<string>> kvp in dic1.Where(kvp => kvp.Value.Contains("ss")))
        {
            kvp.Value.Remove("ss");
        }

There was an answer in a comment that I would like to promote to a valid answer to this question.

in my case I have a Dictionary defined thus:

   Dictionary<string, List<string>> selections = new Dictionary<string, List<string>>();

I am then adding data into the Dictionary based on multi-line textboxes that are user editable. I do not want to allow blank lines if the user edits the field for that will later give me a false count of entries as I am using \\r\\n to split the lines.

        selections.Add("States", txtStates.Text.Replace("\n", "").Split('\r').ToList());
        selections.Add("Zones", txtZones.Text.Replace("\n", "").Split('\r').ToList()); 
        selections.Add("ZipCodes", txtZipCodes.Text.Replace("\n", "").Split('\r').ToList());

By using this line of code I am able to remove all elements that hold no data:

        selections.Values.LastOrDefault(li => { li.Remove(""); return false; });

so that later on I can get an accurate count of the elements from the textbox.

        var Icnt = selections.Where(r => r.Key == "Items").SelectMany(r => r.Value).Count();
        var Scnt = selections.Where(r => r.Key == "States").SelectMany(r => r.Value).Count();
        var Zcnt = selections.Where(r => r.Key == "Zones").SelectMany(r => r.Value).Count();
        var ZCcnt = selections.Where(r => r.Key == "ZipCodes").SelectMany(r => r.Value).Count();

Overall, I found the removal statement to be a very elegant solution.

var newdic = new Dictionary<string, List<string>>();
dic1.ToList().ForEach
(
   pair =>
   {
      newdic.Add(pair.Key, pair.Value.Where(x => x!="ss").ToList());
   }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM