如何删除列表中重复的项目

时间:2018-12-11 14:35:51

标签: c# xamarin.forms

我正在将一个项目添加到列表中,每次退出应用程序并重新输入时,它都会复制该项目,即它会增加列表中的项目

 List<RespCadastro> respCadastro= new List<RespCadastro>();
        var princiapl = new PhoneDetailsGroup()
        {
            phoneNumber = respCadastro.result.registrationForm.contactInformation.contactNumber,
            phoneType = respCadastro.result.registrationForm.contactInformation.primaryContactType + " (principal)"
        };

respCadastro.result.registrationForm.contactInformation.PhoneGroup.Add(principal);
respCadastro.result.registrationForm.contactInformation.PhoneGroup.RemoveAt(0);

1 个答案:

答案 0 :(得分:1)

var princiapl = new PhoneDetailsGroup()
                    {
                        phoneNumber = respCadastro.result.registrationForm.contactInformation.contactNumber,
                        phoneType = respCadastro.result.registrationForm.contactInformation.primaryContactType + " (principal)"
                    };

var count = myList.Count(x => x.phoneNumber == principal.phoneNumber);

if (count > 0) {
  // already exists, don't add
} else {
  // add
}