0

I Have a list and I want to create a new one without records with duplicate values

public List<links> results = new List<links>();
public List<links> final_results = new List<links>();
public class links
{
    public string url { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public int place { get; set; }
}

results gets it's values during code. Same record may occur with unique Place property but other properties are the same. I want to ommit duplicate records which have duplicate value in the url, title and description. I wrote this code but nothing changed:

     final_results = results .GroupBy(n => n.url).Select(g => g.FirstOrDefault()).ToList();

but when I bind my repeator to this new list nothing has changed. how can I ommit this duplicte value?

Luud van Keulen
  • 1,204
  • 12
  • 38
mary
  • 247
  • 3
  • 20
  • 2
    I don't believe that nothing has changed because it should work. But it's not clear if you only want to check the single property `url` or if the combination of the three properties `url`,`title `, `description` is equal. – Tim Schmelter Sep 27 '16 at 14:18
  • Can you post some example data? Do you check the contents of `final_results` or something else? If you check an HTML control you may be binding to the wrong data. – Panagiotis Kanavos Sep 27 '16 at 14:19
  • @mary, I think what you want is a [DistinctBy() Property](http://stackoverflow.com/questions/489258/linqs-distinct-on-a-particular-property) extension method. – jmbmage Sep 27 '16 at 14:20
  • @TimSchmelter I think it is not important which property I check because just Place property is unique . – mary Sep 27 '16 at 14:29
  • @PanagiotisKanavos i use a Repeater and bind it to the list I want to ommit duplicate records – mary Sep 27 '16 at 14:29
  • @jmb.mage distinctby() property is used for records which have all properties the same but in my case I should check some properties – mary Sep 27 '16 at 14:29
  • You could write a class that implements IEqualityComparer by comparing on the properties you are interested in, then use the Linq Distinct() overload that takes the comparer as a parameter: https://msdn.microsoft.com/en-us/library/bb338049(v=vs.110).aspx – Joe Sep 27 '16 at 14:39

3 Answers3

6

If you want to suppress records with equality in 3 preperties url,titleand description, you have to group by all of them:

final_results = results.GroupBy(n => new {n.url, n.description, n.title})
                       .Select(g => g.FirstOrDefault()).ToList();
fubo
  • 44,811
  • 17
  • 103
  • 137
0

Umm, you can just do:

var results = results.DistinctBy(x=> x.url);

Or if you want to exclude by multiple fields, simpliest is this:

var results = results.DistinctBy(x=> x.url + "," + x.title + "," + x.description);

If you want to do it properly - just override Equal and GetHashCode methods of your object and put your values in HashSet.

For DistinctBy to work, you can increase your .NET version or just use MoreLINQ.

eocron
  • 6,885
  • 1
  • 21
  • 50
0

try using Except extension method over the Links

final_results = results.Except(--duplicate criteria--)
Pranay
  • 432
  • 4
  • 14