【问题标题】:Remove items from an Object [] that are contained in a custom class array从自定义类数组中包含的 Object [] 中删除项目
【发布时间】:2011-08-09 14:31:12
【问题描述】:

我有一个自定义项数组,然后我有一个由函数返回的对象数组 object[]。这个对象数组确实是相同的客户端类型,但它作为对象类型的数组返回(我无法编辑这个方面)。

我尝试使用显式强制转换,但在运行时出现异常提示:无法将对象 [] 转换为客户端 []。
一旦我拥有两个数组,我的目标就是从一个列表中删除另一个列表中也存在的项目并建立一个联合,以便拥有一个唯一的列表。

var client = getClients().ToArray();           //Is an array of Clients[]
var normalSearch = getDuplicates().ToArray();  //Is an array of object[]


//This what I try to achieve,but being object, I cannot invoke "c.ContactId" (Line 4)
var uni = (from c in normalSearch
           where !(from d in dupes
                   select d.ContactId)
                   .Contains(c.ContactId)
           select c).ToArray();

我知道如果使用原始类型,Linq Union() 可以自动排除重复,否则必须使用自定义类型开发扩展。但我无法访问其余代码,因此无法更改其他地方的逻辑。

【问题讨论】:

    标签: c# arrays linq list


    【解决方案1】:
    getDuplicates().Cast<Client>();
    

    【讨论】:

      【解决方案2】:
      var uni = client.Union(normalSearch.Cast<Client>())
          .DistinctBy(c => c.ContractId);
      

      DistinctBy 位于MoreLinq

      如果你不能使用 MoreLinq,你可以简单地做

      var uni = client.Union(normalSearch.Cast<Client>())
          .GroupBy(c => c.ContractId)
          .Select(g => g.First());
      

      这基本上是他们的实现。

      【讨论】:

      • 我有一个关于 DistinctBy 的异常,因为我的 IEnumerable 集合没有它的定义。它是在 System.Linq 以外的额外库中还是我必须编写该扩展名?
      • @Luca 它在我链接到的 MoreLinq 库中。
      • @Luca 它是开源的。您可以将 DistinctBy 的源代码复制到您的解决方案中。这是一个有用的扩展。
      【解决方案3】:

      您可以使用 linq 将重复项投射到客户端,然后创建一个比较器类。我从msdn documentation复制并粘贴了这个,可能需要调整

      var normalSearch = getDuplicates().ToArray().Cast<Clients>();
      var client = getClients().ToArray();
      
      var unique = client.Union(normalSearch, new ClientsComparer());
      
      public class ClientsComparer : IEqualityComparer<Clients>
      {
          public bool Equals(Clients x, Clients y)
          {
      
              //Check whether the compared objects reference the same data.
              if (Object.ReferenceEquals(x, y)) return true;
      
              //Check whether any of the compared objects is null.
              if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                  return false;
      
              //Check whether the products' properties are equal.
              return x.ContactId == y.ContactId;
          }
      
          // If Equals() returns true for a pair of objects 
          // then GetHashCode() must return the same value for these objects.
      
          public int GetHashCode(Client client)
          {
              //Check whether the object is null
              if (Object.ReferenceEquals(product, null)) return 0;
      
              //Calculate the hash code for the product.
              return client.ContactId.GetHashCode();
          }
      
      }
      

      【讨论】:

      • 我知道这个解决方案,但是我不能在这部分引入新代码。
      猜你喜欢
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多