1
I’m trying to accomplish the Distinct
, in my model of Phones
, but none of the options I’m trying to make work. Could tell me what I’m doing wrong ?
Below I described several ways I have tried to do, but none of them worked for me, could give me a light ?
model.Phones = model.Phones.Where(x => x != null && x.Phone != null).ToList();
1ª - model.Phones = (from p in model.Phones select p).Distinct().ToList();
2ª - model.Phones = model.Phones.Select(a => a).Distinct().ToList();
I tried it both ways above, nothing else works.
Example of how I receive the data below:
I’ve tried to do it this way:
model.Phones = model.Phones.Select(x => x.Phone).Distinct().ToList();
I get following error:
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<KonBase.Areas.Admin.Models.ApplicationCondominiumViewModels.PhoneViewModel>'
And:
model.Phones = model.Phones.GroupBy(x => x.Phone).ToList();
I get following error:
Cannot implicitly convert type 'System.Collections.Generic.List<System.Linq.IGrouping<string, KonBase.Areas.Admin.Models.ApplicationCondominiumViewModels.PhoneViewModel>>' to 'System.Collections.Generic.List<KonBase.Areas.Admin.Models.ApplicationCondominiumViewModels.PhoneViewModel>'
Does your phone model only have the string attribute? If you have more than one and they have different ones, it’s no use just giving distinct
– Lucas Miranda
Yes, they have only string
– Matheus