识别唯一值在C#列表 [英] Identifying Unique Values in a C# List

查看:151
本文介绍了识别唯一值在C#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,下面,来表示一个复合主键模式:

I have created a class, as below, to represent a Composite Primary Key model:

public class PrimaryKeyModel
{
    public string ColumnName { get; set; }
    public string ColumnValue { get; set; }
    public int RowNumber { get; set; } // always unique
}



它基本上代表了列的名称/值这一起组成了主键,再加上在那里这个组合所属的行数;本来在电子表格

It basically represents the names/values of the columns which together make up the primary key, plus the row number where this combination belongs; originally in a Spreadsheet.

我再放入一个List这个模型并从电子表格的数据填充它:

I have then put this model in a List and populated it with data from a spreadsheet:

List<PrimaryKeyModel> primaryKeysList = new List<PrimaryKeyModel>; 



我想检查primaryKeysList,看看是否有任何重复的值,如果有,我想知道这些值被复制的行号。

I would like to check primaryKeysList and see if it has any duplicated values, and if it has, I would like to know the Row numbers where these values are duplicated.

我已经尝试了不同的方法,如装载这个名单到HashSet的,字典和使用的这个解决方案在这里这个链接但非它的工作。反正是有,我可以解决这个问题。

I have tried different ways such as loading this list into a HashSet, a dictionary and to use this solution here at this link but non of it worked. Is there anyway I could resolve this.

感谢。

更新 - 这是一个简单的数据显示。 UniqueColumnsModel相同PrimaryKeyModel;我在这里改变了它,使它更清晰。

在这里输入的形象描述

编辑:问题

我试图从电子表格中导入数据(它可以有多种类型(一个用于销售,一个报价..等))到数据库中。在数据库中的配置表中的电子表格确定哪些列(S)将构成目标表的主键。我的任务是创建一个使用我的应用程序是它被上传(进口)到数据库之前验证电子表格数据的程序。我想OT验证设置主键的复合列,不包含任何重复的数据,因此,主键约束不是在插入目标表违反..

I am trying to import data from a spreadsheet (which can have many types(one for sales, one for quotes ..etc.)) into a database. A configuration table in the database determines which column(s) in a spreadsheet will constitute the primary key in the destination table. My task is to create a routine which validate spreadsheet data before being it being uploaded (imported) into the database using my application. I want ot validate that the columns set as the composites of the primary key, do not contain any duplicated data, so that the primary key constraint is NOT violated in the destination table on insert..

这里提到的列表(PrimaryKeyModel)包含在电子表格中的列的名称(与他人一起构成主键)在电子表格中的列和在电子表格中的行数时,值,其中这个值存在。该列表被通过一个foreach行/列的foreach循环填充。所以我希望这个阐述的问题更好地

The list mentioned here (PrimaryKeyModel) contains the name of the column in the spreadsheet (which together with others constitutes the primary key), the value of the column in the spreadsheet and the row number in the spreadsheet where this value exists. The list gets populated via a foreach row/ foreach column loops. So I hope this elaborates the question better.

推荐答案

如果你的类表现这样的结构:

If your class represents this kind of structure:

ColumnName    ColumnValue   RowNumber
Id            3             1
Id2           1             1 
Id            1             2 
Id2           2             2
Id            3             3 
Id2           1             3 //duplicate

然后,所有其他的答案为止是不正确的,你需要通过行号做不同的看法,组​​,然后逐个比较各个领域。因为平等是可交换我们可以加快循环小幅所以我们不两次比较每个项目。

Then all other answers so far are incorrect and you need to do it differently, group by row number and then compare each field one by one. Because equality is commutative we can speed up the loop slightly so we don't compare each item twice.

List<PrimaryKeyModel> keys = new List<PrimaryKeyModel>()
{
        new PrimaryKeyModel("Id", "3", 1),
        new PrimaryKeyModel("Id2", "1", 1),
        new PrimaryKeyModel("Id", "1", 2),
        new PrimaryKeyModel("Id2", "1", 2),
        new PrimaryKeyModel("Id", "3", 3),
        new PrimaryKeyModel("Id2", "1", 3),
};

var groupedKeys = keys.OrderBy(pk => pk.ColumnName).GroupBy(k => k.RowNumber).ToList();
HashSet<int> duplicateRowNumbers = new HashSet<int>();

for (int i = 0; i < groupedKeys.Count - 1; i++)
{
    for (int j = i + 1; j < groupedKeys.Count; j++)
    {
        if (AreTheSame(groupedKeys[i], groupedKeys[j]))
        {
            duplicateRowNumbers.Add(groupedKeys[i].First().RowNumber);
            duplicateRowNumbers.Add(groupedKeys[j].First().RowNumber);
        }
    }
}

private static bool AreTheSame(IEnumerable<PrimaryKeyModel> a, IEnumerable<PrimaryKeyModel> b)
{
    var leftEnumerator = a.GetEnumerator();
    var rightEnumerator = b.GetEnumerator();
    while (leftEnumerator.MoveNext() | rightEnumerator.MoveNext())
    {
        if (leftEnumerator.Current == null) return false;
        if (rightEnumerator.Current == null) return false;
        if (leftEnumerator.Current.ColumnValue != rightEnumerator.Current.ColumnValue) return false;
    }

    return true;
}

这篇关于识别唯一值在C#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆