0基础C#学习之路-dt转list<dto>方法

本文介绍了一个将DataTable转换为List<StudentDTO>的方法,通过遍历DataTable中的每一行数据,根据列名获取并设置DTO对象的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的DT转list<dto>方法

public static List<StudentDTO> ConvertDataTableToList<StudentDTO>(DataTable table) where StudentDTO : class, new()
        {
            List<StudentDTO> list = new List<StudentDTO>();

            foreach (DataRow row in table.Rows)
            {
                StudentDTO obj = new StudentDTO();
                foreach (PropertyInfo info in obj.GetType().GetProperties())
                {
                    if (table.Columns.Contains(info.Name))
                    {
                        if (row[info.Name] != DBNull.Value)
                        {
                            info.SetValue(obj, row[info.Name], null);
                        }
                    }
                }
                list.Add(obj);
            }
            return list;
        }

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.it</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.it</groupId> <artifactId>webSocket</artifactId> <version>1.0.0</version> <name>webSocket</name> <description>webSocket</description> <properties> <java.version>17</java.version> <spring-cloud.version>2023.0.3</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Cloud dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <!-- Redis for caching --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Sa-Token for authentication --> <dependency> <groupId>cn.dev33</groupId> <artifactId>sa-token-spring-boot3-starter</artifactId> <version>1.39.0</version> </dependency> <dependency> <groupId>cn.dev33</groupId> <artifactId>sa-token-redis-jackson</artifactId> <version>1.39.0</version> </dependency> <!-- MySQL connector --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- Lombok to reduce boilerplate code --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- Spring Cloud Nacos configuration and discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!-- Spring Cloud OpenFeign for REST clients --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- DTO library --> <dependency> <groupId>com.it</groupId> <artifactId>dto</artifactId> <version>1.0.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <!-- Testing dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- RabbitMQ for messaging --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 排除serlet
最新发布
07-09
### 排除 Maven 项目中的 Servlet 依赖 在使用 Maven 构建项目时,特别是基于 Spring Boot 或其他现代 Java Web 框架(如 Spring WebFlux)的项目中,经常会遇到由于引入了不兼容的 `servlet-api` 依赖而导致的冲突问题。这类问题通常表现为运行时错误或编译失败,例如无法调用 `HttpServletRequest.getPart()` 方法,因为其仅在 Servlet 3.0 及以上版本中可用 [^1]。 为避免此类冲突,可以通过在 `pom.xml` 中使用 `<exclusion>` 标签排除特定依赖项所引入的 `servlet-api`。以下是一个典型的依赖排除示例: ```xml <dependency> <groupId>some.group.id</groupId> <artifactId>some-artifact-id</artifactId> <version>some.version</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency> ``` 通过上述配置,可以有效地阻止该依赖传递性地引入 `servlet-api`,从而避免与容器自带的版本产生冲突 [^3]。 此外,若某个依赖项内部自动下载并引入了不兼容的 `servlet-api`,则可以在该项目的 `<dependencies>` 中显式声明该依赖,并将其 `<scope>` 设置为 `provided`,以确保它不会被打包进最终的 WAR 或 JAR 文件中: ```xml <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> ``` 此方法适用于部署到支持 Servlet 3.x 的容器(如 Tomcat 8+),其中容器本身已提供所需的 API,无需额外打包 [^2]。 在实际开发过程中,推荐使用 Maven Helper 插件的 Dependency Analyzer 功能来可视化分析依赖树,快速定位并解决潜在的依赖冲突问题 [^4]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值