C#在列表中找到最后一项,其中包含完整的一对
我正在使用一个全局密钥记录应用程序(不是病毒,但出于某些合法原因),该应用程序将所有密钥代码和状态存储在一个列表中,例如:C#在列表中找到最后一项,其中包含完整的一对,c#,linq,C#,Linq,我正在使用一个全局密钥记录应用程序(不是病毒,但出于某些合法原因),该应用程序将所有密钥代码和状态存储在一个列表中,例如: public class KeyStatInfo { public int KeyCode; public bool IsDown; public KeyStatInfo(int keyCode, bool isDown) { KeyCode = keyCode; IsDown = isDown; } } p
public class KeyStatInfo {
public int KeyCode;
public bool IsDown;
public KeyStatInfo(int keyCode, bool isDown) {
KeyCode = keyCode;
IsDown = isDown;
}
}
private List<KeyStatInfo> _keyStats;
为了澄清,我需要
\u keyStats
中所有KeyCode
都已关闭的最后一个索引(IsDown
为false
)。如上述注释所示 如果性能确实重要,那么最好在日志记录过程中跟踪所有密钥的状态。可能的解决办法:
KeyStatInfo
:public bool AllKeysAreReleased{get;set;}
。此属性表示保存此stat元素时所有键的状态\u keyState=new dictionary()
。字典中的值显示此时是否释放了键李>
var isAllKeysReleased=\u keyState.Values.Aggregate(true,(acc,i)=>acc&&i)代码>。并将该状态传递给stats缓冲区:\u keyStats.Add(newkeystatinfo(key,isDown,isAllKeysReleased))代码>
\u keyStats.last(i=>i.allkeysareleased)代码>
请编辑问题并阐明您的目标。你想从_keystatsbuffer(可能有一些linq查询)中得到什么?@andrew很抱歉自己不是母语人士。我真的不知道如何更好地描述它。请看一下这个例子好吗?这些评论用解释说明了我的目标。你最好还是使用它。好主意!谢谢但我仍然试图不添加新的变量-除非性能低于可接受水平。
_keyStats = new List<KeyStatInfo>();
_keyStats.Add(new KeyStatInfo(1, true)); // index = 0
_keyStats.Add(new KeyStatInfo(1, false)); // index = 1
// Expect: 1 - the index of the last element, so I know all keys are released
_keyStats.Add(new KeyStatInfo(1, true)); // index = 2
_keyStats.Add(new KeyStatInfo(1, true)); // index = 3
_keyStats.Add(new KeyStatInfo(2, true)); // index = 4
// Expect: 1 as well - the key 1 was pressed since index #2
// Note, key 1 was held since #2, and I need the last index before the first event
_keyStats.Add(new KeyStatInfo(1, false)); // index = 5
// Expect: 3 - although key 1 was released at index #5, key 2 was still unreleased since index #4
_keyStats.Add(new KeyStatInfo(2, false)); // index = 6
// Expect: 6 - all keys are released, return the index of the last element