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();
 

阅读了该文章的人还浏览了

.NET[C#]使用LINQ从List<T>集合中获取最后N条数据记录的方法有哪些?的更多相关文章

  1. resultMap中的collection集合出现只能读取一条数据的解决方法

    查询数据时只能获得collection集合中的的一条数据,相关情况如下: 结果集resultMap: <resultMap id="ManagerRolesAcls" typ ...

  2. 一句话的代码,从集合中找出第一个重复字符的方法javascript版。

    有的时候需求是这样的: 找出集合中第一个重复的字符所在的位置,刚才看了园内某自许为算法的代码,感觉非常之啰嗦故写了以下代码! 本人对神马算法之类的完全不懂,但那些伪算法家们也别出来装蒜.一句话:不要欺 ...

  3. python实例:在列表,字典,集合中,根据条件筛选数据

    1. 从列表中过滤掉 负数 from random import randint # 随机生成列表 data = [randint(-10, 10) for _ in range(10)] print ...

  4. Linq排序,获取前5条数据

    _dic = _dic.OrderByDescending(x => x.Value).ToDictionary(x=>x.Key,x=>x.Value); var Num = _d ...

  5. LINQ对List列表随机排序,取N条数据

    List<Art_Search> artList=new List<Art_Search>(); artList=artList.OrderBy(s => Guid.Ne ...

  6. List<Map<String, Object>>集合中获取某个key并转换为List<Integer>集合

    package com.codyy.sso.controller.yuanqu; import java.util.ArrayList; import java.util.HashMap; impor ...

  7. JDK1.8--体验Stream表达式,从一个对象集合中获取每一个对象的某一个值返回新集合

    xl_echo编辑整理,欢迎转载,转载请声明文章来源.更多IT.编程案例.资料请联系QQ:1280023003 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! --- 开 ...

  8. Struts2把数据封装到集合中之封装到Collection中

    数据封装到集合中,可以封装到集合中,也可以封装到Map中.该篇博客主要讲解数据封装到集合中的封装到Collection中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) ...

  9. 如何在List集合中去重

    众所周知List集合中的元素是有序的,但是List中的元素同样是可以重复的,那么我们应该怎么在List集合中去重呢? 方法一: 对于方法一而言,这也许是一个小窍门.利用的是Set集合中不允许出现重复的 ...

随机推荐

  1. Python从零开始——列表List

    一:Python列表知识总览 二:列表操作符 三:Python内置函数操作列表 四:Python列表封装函数

  2. Ubuntu14 关机重启、版本、网络、防火墙

    关机.重启: 立即关机:halt.poweroff.shutdown -h now 延迟关机:shutdown -h 10  十分钟后关机 立即重启:reboot.shutdown -r now 延迟 ...

  3. 12、shell_awk

    AWK awk 是一个优良的文本处理工具,其名字来源于三个开发人员的名字首字母缩写. awk 不但是一个优良的文件处理工作,它还可以自己编程,编写awk 程序   AWK基本格式:      awk ...

  4. 获取APK的appPackage和appActivity

    [法二]AndroidSDK 此方法是采用AndroidSDK\build-tools\23.0.2(这个版本号可能不一定,但是一般每个版本号里面都,任意即可)\aapt.exe aapt dump ...

  5. Rust自定义智能指针

    深了,真深了. use std::ops::Deref; struct MyBox<T>(T); impl<T> MyBox<T> { fn new(x: T) - ...

  6. src和href 如何做好seo 前端页面有那三层 AMD和CMD 规范的区别 渐进增强

    1==>简述一下src与href的区别 src用于替换当前元素: href用于在当前文档和引用资源之间确立联系 2==>.谈谈以前端角度出发做好SEO需要考虑什么? a. 了解搜索引擎如何 ...

  7. [原创]Appium与Appium desktop的区别

    1.两者都属于Appium 服务端 2.二者最新版本如下:地址:https://github.com/appium/appium-desktop/releases Appium 服务端支持的:地址:h ...

  8. 0.Jenkins 介绍

    一.持续集成的概念 continuous  intergaration  (简称CI),持续集成. 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员每天至少集成一次,也就意味 ...

  9. Spring(004)-Bean装配

    一,问题,Bean找不到 代码 @Component public class DemoClass { public int doSth() { ; } } 测试代码 @RunWith(SpringJ ...

  10. 201777010217-金云馨《面向对象程序设计Java》第八周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...