Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用属性对两个复杂类型的泛型集合进行排序_C#_Generics - Fatal编程技术网

C# 使用属性对两个复杂类型的泛型集合进行排序

C# 使用属性对两个复杂类型的泛型集合进行排序,c#,generics,C#,Generics,我有下面的方法来比较任意给定复杂类型的两个集合。在执行Sequence Equal操作之前,我需要使用所比较类型的给定属性对两个集合进行排序,以确保对象的顺序相同 public static bool CompareCollections<T>(IEnumerable<T> expectedCollection, IEnumerable<T> actualCollection,Func<T,string> selector) {

我有下面的方法来比较任意给定复杂类型的两个集合。在执行Sequence Equal操作之前,我需要使用所比较类型的给定属性对两个集合进行排序,以确保对象的顺序相同

    public static bool CompareCollections<T>(IEnumerable<T> expectedCollection, IEnumerable<T> actualCollection,Func<T,string> selector)
    {
        if (expectedCollection.Count() != actualCollection.Count())
        {
            return false;
        }
        expectedCollection.OrderBy(selector).ToList();
        actualCollection.OrderBy(selector).ToList();
        return expectedCollection.SequenceEqual(actualCollection, new TypeComparer<T>());
    }
当第一个集合和第二个集合如下所示时,请注意这两个集合都有相同的两个对象,但第二个集合的项目顺序相反,我希望OrderBy方法在比较是否相等之前对其排序。但它不像我想象的那样

      var first = new List<Fee>()
      {
        new Fee
        {
            ID = "00001",
            BaseFee = "3.50"
        },
        new Fee
        {
            ID = "00002",
            BaseFee = "5.50"
        }
      };

      var second = new List<Fee>()
      {
        new Fee
        {
            ID = "00002",
            BaseFee = "5.50"
        },
        new Fee
        {
            ID = "00001",
            BaseFee = "3.50"
        }
      };
var first=新列表()
{
新费用
{
ID=“00001”,
BaseFee=“3.50”
},
新费用
{
ID=“00002”,
BaseFee=“5.50”
}
};
var second=新列表()
{
新费用
{
ID=“00002”,
BaseFee=“5.50”
},
新费用
{
ID=“00001”,
BaseFee=“3.50”
}
};

您可以按以下步骤执行:

public static bool CompareCollections<T>(IEnumerable<T> expectedCollection, IEnumerable<T> actualCollection,
                                         Func<T, string> selector)
{
    var expectedAsList = expectedCollection.OrderBy(selector).ToList();
    var actualAsList = actualCollection.OrderBy(selector).ToList();

    return expectedAsList.Count == actualAsList.Count &&
           !expectedAsList.Where((t, i) => !t.Equals(actualAsList[i])).Any();
}
公共静态布尔比较集合(IEnumerable expectedCollection、IEnumerable actualCollection、,
Func选择器)
{
var expectedAsList=expectedCollection.OrderBy(选择器).ToList();
var actualslist=actualCollection.OrderBy(选择器).ToList();
返回expectedAsList.Count==actualAsList.Count&&
!expectedAsList.其中((t,i)=>!t.Equals(actualAsList[i])).Any();
}

您需要将
OrderBy().ToList()
的结果赋给一个新的局部变量。返回一个有序的序列,它不会对传入序列进行适当排序。因此:

var sortedExpectedCollection = expectedCollection.OrderBy(selector);
var sortedActualCollection = actualCollection.OrderBy(selector);

return sortedExpectedCollection.SequenceEqual(sortedActualCollection , new TypeComparer<T>());
var sortedExpectedCollection=expectedCollection.OrderBy(选择器);
var sortedActualCollection=实际Collection.OrderBy(选择器);
返回sortedExpectedCollection.SequenceEqual(sortedActualCollection,new TypeComparer());

如果排序是个问题,只需将值分配给列表并进行比较。

您需要将
expectedCollection.OrderBy(selector).ToList()的返回值分配给一个变量
OrderBy
返回一个新的有序可枚举项,它不会对传入的集合进行适当的排序。噢。。是的,非常感谢你指出。我忽略了这一点!
var sortedExpectedCollection = expectedCollection.OrderBy(selector);
var sortedActualCollection = actualCollection.OrderBy(selector);

return sortedExpectedCollection.SequenceEqual(sortedActualCollection , new TypeComparer<T>());
expectedCollection = expectedCollection.OrderBy(selector).ToList();
actualCollection = actualCollection.OrderBy(selector).ToList();