C# List的用法 -如何用where进行筛选

List< T >定义

泛型集合

  1. 可以通过index访问的强类型对象列表
  2. 提供了搜索、排序、操作的方法
  3. T是列表元素的类型
csharp 复制代码
namespace System.Collections.Generic
{
    //
    // Summary:
    //     Represents a strongly typed list of objects that can be accessed by index. Provides
    //     methods to search, sort, and manipulate lists.
    //
    // Type parameters:
    //   T:
    //     The type of elements in the list.
    [Serializable] //表明该类对象可以被序列化
    [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
    [DebuggerDisplay("Count = {Count}")]
    [__DynamicallyInvokable]
    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>
    {
    public int Count                       //返回元素个数
    public T this[int index]               //通过index 访问
    public void Add(T item)                //添加元素
    public void Insert(int index, T item) //插入元素
    public void RemoveAt(int index)       //通过index删除
    public bool Remove(T item)            //删除
    public void Clear()                   //清空
    public bool Contains(T item)          // 查询是否包含某个对象
    IEnumerator IEnumerable.GetEnumerator() //通过List返回枚举器,
	public int IndexOf(T item)           //查询对象的inx值

Where作为关键字 -对list对象筛选

参考

1. 遍历列表 筛选满足条件的对象

csharp 复制代码
var students = new List<Student>
{
    new Student("ShioriSaginomiya", "20210103", 16, Gender.Female),
    new Student("ShinonomeNano", "20120203", 1, Gender.Female),
    new Student("NoharaShinnosuke", "20200205", 15, Gender.Male),
    new Student("TomCat", "20200529", 19, Gender.Male),
    new Student("JerryMouse", "20210529", 19, Gender.Female),
    new Student("Enmu", "20190303", 2333, Gender.Male),
    new Student("Tohru", "20200101", 18, Gender.Female),
    new Student("KannaKamui", "20210101", 16, Gender.Female),
    new Student("HirasawaYui", "20150201", 28, Gender.Female),
    new Student("NakanoAzusa", "20160201", 26, Gender.Female)
}; 
csharp 复制代码
var adults =
    from student in students
    where student.Age >= 18
    select student;
csharp 复制代码
foreach (var student in students)
    if (student.Age >= 18)
        yield return student; 

2. 紧挨着的 where 从句,多个条件筛选

csharp 复制代码
var student =
    from student in students
    where student.Age >= 18
    where student.Chinese + student.English + student.Math >= 180
    select student.Name; 

泛型集合

在C#中,非泛型集合主要指的是早期.NET框架中使用的集合类型,它们没有使用泛型参数来指定存储元素的类型。这些集合类型通常位于System.Collections命名空间中。以下是一些常见的非泛型集合类型:

  1. ArrayList:动态数组,可以存储任何类型的对象。
  2. Hashtable:键值对集合,使用哈希表实现,可以存储任何类型的键和值。
  3. Stack:后进先出(LIFO)的集合,用于实现堆栈数据结构。
  4. Queue:先进先出(FIFO)的集合,用于实现队列数据结构。
  5. SortedList:键值对集合,元素按照键排序存储。
  6. BitArray:位数组,可以存储布尔值,每个位可以独立设置为true或false。
    这些非泛型集合类型在.NET 1.1及之前的版本中广泛使用,但随着.NET

2.0的发布,引入了泛型集合,它们提供了类型安全、性能提升以及更好的编译时检查。

泛型集合类型位于System.Collections.Generic命名空间,例如List<T>Dictionary<TKey, TValue>等。
尽管泛型集合提供了很多优势,但在某些情况下,非泛型集合仍然有其应用场景,比如当你需要存储不同类型的对象时。然而,通常推荐使用泛型集合,因为它们提供了更好的类型安全和性能。

Enumerable提供一系列静态方法,用于查询对象(实现了ystem.Collections.Generic.IEnumerable接口的对象)

而List刚好

System.Linq.Enumerable - Where定义

泛型集合是指通过传入参数类型T 后再定义的集合

静态类Enumerable 提供一系列静态方法,用于查询对象(实现了ystem.Collections.Generic.IEnumerable接口的对象)

List实现了IEnumerable接口,所以可以调用下面的静态函数

在C#中,System.Linq.Enumerable 类提供了一组扩展方法,这些方法可以对实现了 IEnumerable<T> 接口的集合类型进行操作。这些方法使得你可以以声明性方式处理集合数据,而不需要编写复杂的循环代码。以下是一些常用的 Enumerable 静态函数及其作用:

csharp 复制代码
namespace System.Linq

    public static class Enumerable
    {
//定义了一系列静态函数
  1. Where:过滤集合中的元素,返回满足条件的元素。

    csharp 复制代码
    var evenNumbers = numbers.Where(x => x % 2 == 0);
  2. Select:对集合中的每个元素应用一个转换函数,返回转换后的元素集合。

    csharp 复制代码
    var squares = numbers.Select(x => x * x);
  3. SelectMany:将集合中的每个元素展开成一个新的集合,并将所有集合合并为一个集合。

    csharp 复制代码
    var flattened = items.SelectMany(x => x.SubItems);
  4. OrderBy / OrderByDescending:对集合进行升序或降序排序。

    csharp 复制代码
    var sorted = people.OrderBy(x => x.Name);
  5. ThenBy / ThenByDescending:在现有排序基础上进一步排序。

    csharp 复制代码
    var sorted = people.OrderBy(x => x.Age).ThenBy(x => x.Name);
  6. GroupBy:将集合中的元素按指定键分组。

    csharp 复制代码
    var groups = people.GroupBy(x => x.Country);
  7. Aggregate:对集合中的元素进行累加操作,返回单一结果。

    csharp 复制代码
    int sum = numbers.Aggregate((a, b) => a + b);
  8. Count:返回集合中的元素数量。

    csharp 复制代码
    int count = numbers.Count();
  9. Any / All:检查集合中是否有任何元素满足条件或所有元素都满足条件。

    csharp 复制代码
    bool hasAny = numbers.Any();
    bool allEven = numbers.All(x => x % 2 == 0);
  10. Contains:检查集合中是否包含某个特定的元素。

    csharp 复制代码
    bool contains = numbers.Contains(5);
  11. DefaultIfEmpty:如果集合为空,则返回一个包含默认值的集合。

    csharp 复制代码
    var empty = numbers.DefaultIfEmpty(0);
  12. Distinct:返回集合中不重复的元素。

    csharp 复制代码
    var unique = numbers.Distinct();
  13. First / FirstOrDefault:返回集合中的第一个元素或默认值。

    csharp 复制代码
    var first = numbers.First();
  14. Last / LastOrDefault:返回集合中的最后一个元素或默认值。

    csharp 复制代码
    var last = numbers.Last();
  15. Single / SingleOrDefault:返回集合中唯一的元素或默认值。

    csharp 复制代码
    var single = numbers.Single();
  16. Skip / Take:跳过集合中的前N个元素或取集合中的前N个元素。

    csharp 复制代码
    var skipped = numbers.Skip(5);
    var taken = numbers.Take(3);

这些方法使得对集合进行查询和处理变得更加简洁和直观。使用LINQ(Language Integrated Query)可以极大地提高代码的可读性和维护性。

相关推荐
__water8 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water8 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
程序猿小D8 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa
__water8 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
iummature10 小时前
ZLMediaKit Windows编译以及使用
windows
__water11 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。12 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug12 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主13 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
Echo_Lee013 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#