Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想创建一个通用扩展方法,它将接收 IQueryable 并以线程安全的方式在单独的线程/任务上执行操作(如获取前十个元素)。
有人可以给我一个正确方法的代码示例吗
谢谢
像这样的东西
public static List<T> GetTopN<T>(IQueryable<T> inData, int n) { List<T> outData = new List<T>(n); lock (inData) { int i = 0; foreach (T t in inData) { if (i >= n) { return outData; } i++; outData.Add(t); } } return outData; }