Google and StackOverflow are your friends....
Code: Select all
dict = dict.Where(d => d.Value != nValue).ToDictionary(t => t.Key, t=> t.Value);
Post by NickFriend »
Code: Select all
dict = dict.Where(d => d.Value != nValue).ToDictionary(t => t.Key, t=> t.Value);
Nick Friend wrote:I don't know why I bothered doing this, but anyway....
Dictionary with 1 million items, every other one is removed.
Method 1 = 00:00:00.1022951 (Wolfgang's original)
Method 2 = 00:00:00.0773887 (Robert's second)
Method 3 = 00:00:00.1158190 (mine)
In other words it doesn't make the slightest difference to speed (we're talking a million items here) which technique you use.
Your choice, 9 lines of code or 1.
Nick
Post by NickFriend »
Post by Phil Hepburn »
Code: Select all
local implied oDict := Dictionary<string, string>{}
//--- key, value ---
oDict:Add("txt", "notepad.exe")
oDict:Add("bmp", "paint.exe")
oDict:Add("dib", "paint.exe")
oDict:Add("rtf", "wordpad.exe")
oDict:Add("ybmp", "ypaint.exe")
oDict:Add("zdib", "zpaint.exe")
oDict:Add("artf", "awordpad.exe")
oDict:Add("btxt", "bnotepad.exe")
oDict:Add("cbmp", "cpaint.exe")
oDict:Add("ddib", "dpaint.exe")
oDict:Add("ertf", "ewordpad.exe")
MessageBox.Show(oDict:GetType():ToString(),"Dict type Original :")
oDict := (from d in oDict where d:Key <> "txt" where d:Value <> "paint.exe" select d) ;
:ToDictionary({|r| r.Key}, {|r| r.Value})
MessageBox.Show(oDict:GetType():ToString(),"Dict type Result :")
MessageBox.Show(oDict:Count():ToString(),"Dict count :")
Post by Phil Hepburn »
Code: Select all
oDict := oDict:Where( {|Item| Item:Value != nValue } ):ToDictionary( {|Item| Item:Key }, {|Item| Item:Value } )
Code: Select all
foreach oItem as KeyValuePair<string,int> in oDict:ToList()
if oItem:Value == nValue
oDict:Remove( oItem:Key )
endif
next
Post by Phil Hepburn »
Post by Phil Hepburn »