【发布时间】:2014-03-05 20:28:35
【问题描述】:
对于这个模棱两可的标题,我深表歉意。我无法同时保持清晰和简洁。所以请随意更改它。
我有一个包含其他几个列表的大列表。而这些内部 List 包含 Column 对象。
List<List<Column>> listOfAllColumns;
假设我的内部列表包含不同的 Column 对象,如下所示:
list1 = {c1, c1, c2}
list2 = {c1, c2, c1}
list3 = {c2, c3}
list4 = {c1,c1, c2}
大列表包含以下列表: listOfAllColumns = {list1, list2, list3, list4}
现在我想要一个从 listOfAllColumns 列表中删除 重复列表 的方法。例如,它将查看上面的列表并删除 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} 不同。
【问题讨论】:
-
听起来您需要查看 Distinct 的重载,因此您可以传入您的比较器对象。
-
@RBarryYoung 现在我想要一个从 listOfAllColumns 列表中删除重复列表的方法。
-
@gunr2171 谢谢。它适用于对象列表吗?