从list< object>的列表中删除重复的内部列表.在C#中 [英] Remove duplicate inner lists from a list of list<object> in c#
问题描述
我为模糊的标题表示歉意.我不能同时保持简洁明了.因此,请随时进行更改.
I apologize for the ambiguous title. I couldn't keep it clear and concise at the same time. So feel free to change it.
我有一个很大的列表,其中包含其他几个列表.这些内部列表包含Column对象.
I have a big List which contains several other Lists. And these inner Lists contain Column objects.
List<List<Column>> listOfAllColumns;
假设我的内部列表包含不同的Column对象,如下所示:
Let's say my inner lists contain different Column objects like this:
list1 = {c1, c1, c2}
list2 = {c1, c2, c1}
list3 = {c2, c3}
list4 = {c1,c1, c2}
大列表包含以下列表: listOfAllColumns = {list1,list2,list3,list4}
And the big list contains these lists: listOfAllColumns = {list1, list2, list3, list4}
现在,我需要一种从listOfAllColumns列表中删除重复列表的方法.例如,它将查看上面的列表并删除list4.
Now I want a method that removes duplicate lists from the listOfAllColumns list. For example, it will look into the list above and remove list4.
list1: c1,c1,c2
list2: c1,c2,c1
list3: c2,c3
list4: c1,c1,c2 (it is equal to list1 so it is a duplicate)
这是我的代码:
public class ColumnList
{
public void RemoveDuplicateColumnTypes()
{
Column c1 = new Column() { SectionName = "C50", StirrupType = "Tie" };
Column c2 = new Column() { SectionName = "C50", StirrupType = "Spiral" };
Column c3 = new Column() { SectionName = "C40", StirrupType = "Tie" };
List<Column> list1 = new List<Column>() { c1, c1, c2 };
List<Column> list2 = new List<Column>() { c1, c2, c1 };
List<Column> list3 = new List<Column>() { c2, c3 };
List<Column> list4 = new List<Column>() { c1, c1, c2 };
List<List<Column>> listOfAllColumns = new List<List<Column>>() { list1, list2, list3, list4 };
var result = listOfAllColumns.Distinct();
}
}
class Column
{
public string SectionName;
public string StirrupType;
public int StirrupSize;
public double StirrupSpacing;
}
顺便说一句,顺序很重要,例如{c1,c2,c1}与{c2,c1,c1}不同.
By the way the order is important, so for example {c1, c2, c1} is different than {c2,c1,c1}.
推荐答案
我将实现一个自定义的IEqualityComparer<IEnumerable<Column>>
,您可以将其用于Distinct
:
I would implement a custom IEqualityComparer<IEnumerable<Column>>
which you can use for Distinct
:
public class ColumnListComparer : IEqualityComparer<IEnumerable<Column>>
{
public bool Equals(IEnumerable<Column> x, IEnumerable<Column> y)
{
if (x == null || y == null) return false;
if (object.ReferenceEquals(x, y)) return true;
return x.SequenceEqual(y);
}
public int GetHashCode(IEnumerable<Column> obj)
{
unchecked
{
int hash = 17;
foreach(Column col in obj)
{
hash = hash * 23 + (col == null ? 0 : col.GetHashCode());
}
return hash;
}
}
}
现在可行:
var result = listOfAllColumns.Distinct(new ColumnListComparer());
您还需要在类Column
中覆盖Equals
+ GetHashCode
:
You also need to override Equals
+ GetHashCode
in your class Column
:
public class Column
{
public string SectionName;
public string StirrupType;
public int StirrupSize;
public double StirrupSpacing;
public override bool Equals(object obj)
{
Column col2 = obj as Column;
if(col2 == null) return false;
return SectionName == col2.SectionName
&& StirrupType == col2.StirrupType
&& StirrupSize == col2.StirrupSize
&& StirrupSpacing == col2.StirrupSpacing;
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + (SectionName ?? "").GetHashCode();
hash = hash * 23 + (StirrupType ?? "").GetHashCode();
hash = hash * 23 + StirrupSize.GetHashCode();
hash = hash * 23 + StirrupSpacing.GetHashCode();
return hash;
}
}
}
这篇关于从list< object>的列表中删除重复的内部列表.在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!