本文介绍了尝试使用除一个IEnumerable结构元素排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个IEnumerable的一个IEnumerable类型删除元素。

I am trying to remove elements from an IEnumerable type from another IEnumerable.

下面我得到的完整列表。

Here I get the complete list

var tiposObj = from t in context.sistema_DocType
                               select
                                   new tgpwebged.Models.SettingsModels.TipoIndiceModel
                                   {
                                       id = t.id,
                                       tipo = t.tipoName
                                   };

                classificacaoModel.tipos = tiposObj.ToList();

和这里的部分​​列表可以从第一排除

And here a partial list to be excluded from the first

  var tiposAtribuidosObj = from t in context.sistema_DocType
                                        join c in context.sistema_ClassificacaoTipo on t.id equals c.idTipo
                                        where c.idClassificacao == classificacaoId
                                        select new tgpwebged.Models.SettingsModels.TipoIndiceModel
                                        {
                                            id = t.id,
                                            tipo = t.tipoName
                                        };
                classificacaoModel.tiposAtribuidos = tiposAtribuidosObj.ToList();   

下面是我如何我不包括:

Here is how I am excluding:

classificacaoModel.tiposNaoAtribuidos = classificacaoModel.tipos.Except(classificacaoModel.tiposAtribuidos);

没有元素被排除在第一个列表。不容找出原因。它们具有相同的结构和相同的类型。

No elements is excluded from the first list. Can´t figure out why. They have same structure and same types.

推荐答案

.NET框架有没有办法比较 TipoIndiceModel 2的实例。
为此,必须实现或派生自 EqualityComparer

.NET framework has no way to compare 2 instances of TipoIndiceModel.For this you have to implement IEqualityComparer or derive from EqualityComparer.

提示上:

此接口仅支持相等比较。定制
  分类和排序比较是由提供的IComparer
  通用接口。

我们建议您从EqualityComparer类派生
  而不是实现的IEqualityComparer接口,因为
  在EqualityComparer类测试用平等
  IEquatable.Equals方法代替的Object.Equals方法。这个
  是一致的包含,的IndexOf,LastIndexOf和Remove
  Dictionary类等通用的方法
  集合。

We recommend that you derive from the EqualityComparer class instead of implementing the IEqualityComparer interface, because the EqualityComparer class tests for equality using the IEquatable.Equals method instead of the Object.Equals method. This is consistent with the Contains, IndexOf, LastIndexOf, and Remove methods of the Dictionary class and other generic collections.

这篇关于尝试使用除一个IEnumerable结构元素排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:17