0

Here I have a list of Column objects. Each Column object has several properties that may be unique or not. Here is an example code I have written so far.How can I remove the duplicates from columns list so that in the final list I only have c1 and c2?

Program.cs

var pc1 = new PartialColumn { Section = "C50" };
var pc2 = new PartialColumn { Section = "C40" };

var c1 = new Column
{
    X = 0,
    Y = 0,
    StartElevation = 0,
    EndElevation = 310,
    FoundationHeight = 60,
    ListOfPartialColumns = new List<PartialColumn> {pc1}
};

var c2 = new Column
{
    X = 600,
    Y = 0,
    StartElevation = 0,
    EndElevation = 630,
    FoundationHeight = 60,
    ListOfPartialColumns = new List<PartialColumn> { pc1, pc2 }
};

var c3 = new Column
{
    X = 600,
    Y = 0,
    StartElevation = 0,
    EndElevation = 630,
    FoundationHeight = 60,
    ListOfPartialColumns = new List<PartialColumn> { pc1, pc2 }
};

var columns = new List<Column> {c1, c2, c3};

PartialColumn.cs

public class PartialColumn
{
    public string Section { get; set; }
}

Column.cs

public class Column
{
    public double StartElevation { get; set; }
    public double EndElevation { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
    public List<PartialColumn> ListOfPartialColumns { get; set; }
    public double FoundationHeight { get; set; }
}

Update:

I want to remove the duplicates from the columns list. As you can see, c2 and c3 have the same properties. I want to remove c3.

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • you should tell according to what you want to group those objects. And actually by grouping items you won't get reduced number of elements. that's just not what it is. You should use `where` for filtering some items. – Tolga Evcimen May 03 '14 at 07:36
  • @CameronMacFarland The problem is that I have another List as property for my `Column` class that has made me confused. – Vahid May 03 '14 at 08:18

1 Answers1

2

Your question How can I group ... seems odd. It appears that you want to How can I select .... Next I would ask how you want to group/select to only get c1 and c2. There are no properties that are common to c1 and c2 and not to c3.

If you wanted to get c2 and c3, then try:

columns
   .Where(c => c.EndElevation == 630)

Answer to updated question

The standard way to get distinct values was answered long ago at Remove duplicates in the list using linq.

What you need to do is to add an comparator for the Column class and then try:

var distinctColumns = columns
   .Distinct(new ColumnComparer()); 
Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • I want to remove the duplicates from the `columns` list. As you can see, c2 and c3 have the same properties. I want to remove c3. – Vahid May 03 '14 at 07:37
  • Should I overwrite the GetHashCode()` and `Equals()` for the `Column` class too? – Vahid May 03 '14 at 08:07
  • 1
    That's is another approach. By using a specific comparator you can avoid that head-ache. – Richard Schneider May 03 '14 at 09:51
  • Thanks Richard, please look at my other question There I have explained myself better with the full code. Unfortunately my question is ignored and tagged as a duplicate without being observed carefully. http://stackoverflow.com/questions/23442831/remove-duplicates-from-a-listobject-using-linq-in-c-sharp – Vahid May 03 '14 at 09:54