.NET[C#]使用LINQ从List<T>集合中获取最后N条数据记录的方法有哪些?
2024-08-26 21:29:35
https://codedefault.com/2018/using-linq-to-get-the-last-n-elements-of-a-collection-in-csharp-application
方案一
collection.Skip(Math.Max(0, collection.Count() - N));
我们也可以把它写成一个静态扩展方法,如:
public static class MiscExtensions
{
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int N)
{
return source.Skip(Math.Max(0, source.Count() - N));
}
}
调用方法:
collection.TakeLast(5);
方案二
coll.Reverse().Take(N).Reverse().ToList();
静态扩展类如:
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> coll, int N)
{
return coll.Reverse().Take(N).Reverse();
}
调用方法:
coll.TakeLast(5);
如果不想使用静态扩展方法,还可以使用 Enumerable.Reverse() 方法,如下:
List<string> mystring = new List<string>() { "one", "two", "three" };
mystring = Enumerable.Reverse(mystring).Take(2).Reverse().ToList();
方案三
public static class Extensions
{
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> collection,
int n)
{
if (collection == null)
throw new ArgumentNullException("collection");
if (n < 0)
throw new ArgumentOutOfRangeException("n", "n must be 0 or greater");
LinkedList<T> temp = new LinkedList<T>();
foreach (var value in collection)
{
temp.AddLast(value);
if (temp.Count > n)
temp.RemoveFirst();
}
return temp;
}
}
调用方法:
IEnumerable<int> sequence = Enumerable.Range(1, 10000);
IEnumerable<int> last10 = sequence.TakeLast(10);
方案四
public static class TakeLastExtension
{
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int takeCount)
{
if (source == null) { throw new ArgumentNullException("source"); }
if (takeCount < 0) { throw new ArgumentOutOfRangeException("takeCount", "must not be negative"); }
if (takeCount == 0) { yield break; }
T[] result = new T[takeCount];
int i = 0;
int sourceCount = 0;
foreach (T element in source)
{
result[i] = element;
i = (i + 1) % takeCount;
sourceCount++;
}
if (sourceCount < takeCount)
{
takeCount = sourceCount;
i = 0;
}
for (int j = 0; j < takeCount; ++j)
{
yield return result[(i + j) % takeCount];
}
}
}
调用方法:
List<int> l = new List<int> {4, 6, 3, 6, 2, 5, 7};
List<int> lastElements = l.TakeLast(3).ToList();
方案五
public static IEnumerable<T> FilterLastN<T>(this IEnumerable<T> source, int n, Predicate<T> pred)
{
int goldenIndex = source.Count() - n;
return source.SkipWhile((val, index) => index < goldenIndex && pred(val));
}
方案六
IEnumerable<int> source = Enumerable.Range(1, 10000);
IEnumerable<int> lastThree = source.AsObservable().TakeLast(3).AsEnumerable();
阅读了该文章的人还浏览了
最新文章
- office快速制作简历
- 第 21 章 CSS3 文本效果
- 【转载】Deep Learning(深度学习)学习笔记整理
- hdoj1325 Is It A Tree?
- 酷狗音乐QQ显示(VC源代码)
- Emit技术使用实例及应用思路
- 初始Socket编程(python)
- Chrome development tools学习笔记(5)
- this语句
- 细数Python Flask微信公众号开发中遇到的那些坑
- Java之Iterator
- Nunit的尝试
- LOJ6089 小Y的背包计数问题(根号优化背包)
- Trinity的分步运行
- WebMagic编译时提示Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.18的解决方法
- websocket 群聊单聊
- cf 366D D. Dima and Trap Graph (计算所有线段共同覆盖的某段区间)
- CodeFrist、ModelFirst、DatabaseFirst
- HTML之绝对路径与相对路径
- 安装Memcache的PHP扩展
热门文章
- tensorflow模型量化实例
- css 序
- ppm
- ssh登录缓慢,输入账户密码等待时间长
- 201871010101-陈来弟《面向对象程序设计(JAVA)》 第14周学习总结
- 201871010104-陈园园 《面向对象程序设计(java)》第十二周学习总结
- Mac环境下 jieba 配置记录
- (translation.E004) You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUAGES setting.
- LeetCode 100. Same Tree相同的树 (C++)
- Mysql 视图&;事务&;触发器