Cie*_*iel 5 c# linq dictionary
鉴于代码..
var dictionary = new Dictionary<string, string>{
{ "something", "something-else" },
{ "another", "another-something-else" }
};
dictionary.ForEach( item => {
bool isLast = // ... ?
// do something if this is the last item
});
Run Code Online (Sandbox Code Playgroud)
我基本上想看看我在ForEach迭代中使用的项目是否是字典中的最后一项.我试过了
bool isLast = dictionary[ item.Key ].Equals( dictionary.Last() ) ? true : false;
Run Code Online (Sandbox Code Playgroud)
但那不起作用......
Chr*_*lsh 10
Dictionary.Last返回一个KeyValuePair,然后将其与一个键的值进行比较.你需要检查:
dictionary[item.Key].Equals( dictionary.Last().Value )
IAbstract也是正确的,您可能需要使用OrderedDictionary.