0

我有一个关于从数组列表中删除重复项的问题。我想连接两个表中的两个字符串,然后拆分它。将一个表中的字符串与其他表进行比较,如果存在重复,则将其删除。我用这段代码做到了,但我问是否有另一种更快更短的方法来做到这一点?

表格1:String a = "abc"

表2:String b = "def,abc,okl"

结果:"abc,def,okl"

我的代码:

foreach(DataRow tr in transM.Rows)
{
    foreach(DataRow tb in tableDGV1.Rows)
    {
        if (tr["ID"].ToString() == tb["ID"].ToString())
        {
            string trMitter = tr["Tr"].ToString() + "," + tb["Tr"].ToString();
            string[] trSplit = trMitter.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            List<string> listTr = new List<string>(trSplit);

            for (int k = 0; k < listTr.Count; k++)
            {
                for (int g = k + 1; g < listTr.Count; g++)
                {
                    if (listTr[g].ToString() == listTr[k].ToString()) listTr.RemoveAt(g);
                }
            }
        }
    }
}
4

5 回答 5

2

我您使用的是 .Net V 3.5 或更高版本,您可以简单地使用

var firstSource = firstString.Split(','); 
var secondeSource = secondString.Split(',');
var result = firstSource.Union(secondSource);`

或使用.Distinct()

foreach(DataRow tr in transM.Rows)
{
    foreach(DataRow tb in tableDGV1.Rows)
    {
        if (tr["ID"].ToString() == tb["ID"].ToString())
        {
            string trMitter = tr["Tr"].ToString() + "," + tb["Tr"].ToString();
            string[] trSplit = trMitter.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

            IEnumerable<string> noDuplicate = trSplit.Distinct();              

        }
    }
}

或者更多的 Linqish 风格:

var noDuplicate = from DataRow row1 in transM.Rows
                  from DataRow row2 in tableDGV1.Rows
                  where row1["ID"].Equals(row2["ID"])
                  let array1 = row1["tr"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)
                  let array2 = row2["tr"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)
                  select array1.Union(array2).Distinct();
于 2012-11-27T10:25:00.090 回答
0

您可以使用:Enumerable.Union,它使用默认的相等比较器生成两个序列的集合并集。

        string[] id1 = { "abc" };
        string[] id2 = { "def", "abc", "okl" };

        IEnumerable<string> both = id2.Union(id1);

        foreach (string id in both)
            Console.WriteLine(id);
于 2012-11-27T10:51:55.620 回答
0

您可以首先创建一个包含所有元素的列表,对列表进行排序并删除重复项。这里有一个相关的问题:Remove duplicates from array

于 2012-11-27T10:34:25.623 回答
0

我怀疑最大的性能损失是在表行的循环上。
您可以使用此优化查询(如果 ID 列是主键)

foreach(DataRow tr in transM.Rows)
{
    string idValue = tr["ID"].ToString();
    DataRow[] foundRows = tableDGV1.Select("ID = " + idValue);
    if(foundRows.Length == 1)
    {
       // Here you have the internal row to join with the external one
        string joinedString = tr["Tr"].ToString() + "," + foundRows[0]["Tr"].ToString();
        string[] trSplit = joinedString.Split(new char[] {','}, 
                           StringSplitOptions.RemoveEmptyEntries);
        List<string> listTr = trSplit.Distinct().ToList();                        
        .........
    }
}

这将删除内部表上不必要的循环

于 2012-11-27T10:31:23.873 回答
0

Enumerable.Union()方法删除重复项:

var items1 = new[] { "abc" };
var items2 =  "def,abc,okl";

var splitItems2 = items2.Split(',');

var result = 
    items1.Union(splitItems2) // Removes duplicates
    .ToArray();
于 2012-11-27T10:45:18.663 回答