Reputation: 11
I have a dictionary of type (string, string) which is a list of groups and their IDs. I need the 2nd dictionary which should not have five groups (which I know). Can anyone help me in framing the select in LinQ which I can use to create this sub dictionary?
Upvotes: 0
Views: 86
Reputation: 18051
I think this should to the trick:
Dictionary<string, string> myDic;
...
var badGroups = new[] { "badGroup1", "badGroup2", ... };
var my2ndDic = myDic
.Where(e => !badGroups.Contains(e.Key))
.ToDictionary(e => e.Key, e.Value);
I suppose what you missed was the .ToDictionary() method.
Upvotes: 0
Reputation: 3173
var groups = new List<string>();
// Fill your groups
var yourDict = new Dictionary<string, string>();
//fill your dict.
var filteredDict = yourDict.Where(a => !groups.Contains(a.Key)).ToDictionary(k=>k.Key,v=>v.Value);
Upvotes: 0
Reputation: 73502
var myDic = new Dictionary<string,string>();
myDic.Add("1","One");
myDic.Add("2","Two");
myDic.Add("3","Three");
myDic.Add("4","Four");
//myDic.Dump();
var exclusions = new []{"2","3"};
var newDict = myDic.Where(x=> !exclusions.Contains(x.Key))
.ToDictionary(x=> x.Key, x=> x.Value);
//newDict.Dump();
Upvotes: 1