首页 > 解决方案 > 使用 C# Fluent Assertions 比较具有不同大小的相同 DTO 列表

问题描述

当我有两个包含相同对象类型但大小不同的列表时,我遇到了这个问题。我想确保根据给定 DTO 的所有属性检查它们(例如一个是另一个的子列表),例如:

public class Student
{
    public int? id { get; set; }
    public string? name { get; set; }
}

例如,我有两个列表:

List<Student> firstList = new List<Student>() { 
    new Student(){ Id = 1, Name="Bill"},
    new Student(){ Id = 2, Name="Steve"},
    new Student(){ Id = 3, Name="Ram"},
    new Student(){ Id = 4, Name="Abdul"}
};

List<Student> secondList = new List<Student>() { 
    new Student(){ Id = 1, Name="Bill"},
    new Student(){ Id = 4, Name="Abdul"}
};

所以我的问题是如何使用 Fluent Assertion 断言 secondList 是 firstList 的子列表,使用深度比较确保对象的所有属性都相互比较。

感谢您的时间和关注!

标签: c#assertion

解决方案


推荐阅读