【C# 】使用List<实体类>

1. 使用List<实体类>

要在C#中使用List<EntityTemp>并实现查找数据输出,首先需要定义EntityTemp类,并创建一个List<EntityTemp>类型的列表。然后,你可以使用LINQ或其他方法来查找和输出数据。

假设EntityTemp类具有一个名为Name的属性,我们将基于此属性进行查找。以下是一个示例:

1.1 定义EntityTemp

cs 复制代码
public class EntityTemp
{
    public string Name { get; set; }
    public int Value { get; set; }
}

1.2 创建List<EntityTemp>类型的列表并添加一些元素

cs 复制代码
List<EntityTemp> entities = new List<EntityTemp>
{
    new EntityTemp { Name = "Item1", Value = 10 },
    new EntityTemp { Name = "Item2", Value = 20 },
    new EntityTemp { Name = "Item3", Value = 30 }
};

1.3 查找特定的数据并输出

假设我们要查找名称为"Item2"的实体,并输出其Value属性。

cs 复制代码
EntityTemp entity = entities.FirstOrDefault(e => e.Name == "Item2");

if (entity != null)
{
    Console.WriteLine($"Found entity with Name '{entity.Name}' and Value {entity.Value}.");
}
else
{
    Console.WriteLine("No matching entity found.");
}

在这个例子中,我们使用FirstOrDefault方法来查找列表中名称为"Item2"的实体。如果找到了匹配的实体,我们将输出它的NameValue属性;如果没有找到,我们将输出一条消息表示未找到。

下面是完整的示例代码:

cs 复制代码
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        List<EntityTemp> entities = new List<EntityTemp>
        {
            new EntityTemp { Name = "Item1", Value = 10 },
            new EntityTemp { Name = "Item2", Value = 20 },
            new EntityTemp { Name = "Item3", Value = 30 }
        };

        EntityTemp entity = entities.FirstOrDefault(e => e.Name == "Item2");

        if (entity != null)
        {
            Console.WriteLine($"Found entity with Name '{entity.Name}' and Value {entity.Value}.");
        }
        else
        {
            Console.WriteLine("No matching entity found.");
        }
    }
}

public class EntityTemp
{
    public string Name { get; set; }
    public int Value { get; set; }
}

运行这段代码,你将看到如下输出:

复制代码
Found entity with Name 'Item2' and Value 20.

2. 对List<T>取交集、差集以及并集

C# 对List<T>取交集、差集以及并集_c# list 差集-CSDN博客文章浏览阅读2.6k次。C# 对List取交集、差集以及并集_c# list 差集https://blog.csdn.net/wangnaisheng/article/details/131391464

3. 快速把List<string>集合转换成一个字符串

C# 快速把List<string>集合转换成一个字符串_list 转字符串-CSDN博客文章浏览阅读939次。C# 快速把List集合转换成一个字符串_list 转字符串https://blog.csdn.net/wangnaisheng/article/details/138717158

4. 一个List<T> 如何快速的知道里面的最大、最小、平均值呢?

【C# 】 一个List<T> 如何快速的知道里面的最大、最小、平均值呢?_c#获取list集合中的最大最小值-CSDN博客文章浏览阅读335次。【C# 】 一个List 如何快速的知道里面的最大、最小、平均值呢?_c#获取list集合中的最大最小值https://blog.csdn.net/wangnaisheng/article/details/140514960

相关推荐
mudtools3 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫7 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫1 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl2 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#