Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c将通用列表项添加到另一个现有列表项#_C# - Fatal编程技术网

C# 使用c将通用列表项添加到另一个现有列表项#

C# 使用c将通用列表项添加到另一个现有列表项#,c#,C#,我有一个通用的清单,里面有很多项目。在这个列表中,我有一列是唯一的(比如ID)。我还有一个通用列表,包含其他项目和相同的ID列。这是我将项目填入列表的方式: foreach (string s in l1) { GridViewSource src = new GridViewSource(); src.test = "id" + s; list.Add(src); } foreach (string s in l2) { GridViewSource src

我有一个通用的清单,里面有很多项目。在这个列表中,我有一列是唯一的(比如ID)。我还有一个通用列表,包含其他项目和相同的ID列。这是我将项目填入列表的方式:

foreach (string s in l1)
{
    GridViewSource src = new GridViewSource();
    src.test = "id" + s;

    list.Add(src);
}

foreach (string s in l2)
{
    GridViewSource src = new GridViewSource();
    src.test = "id" + s;
    src.test2 = "somerandomtext" + s;

    list2.Add(src);
}
我试图做的是将
list2
中的项目添加到
list
中,如果它们具有相同的ID。因此,它应该将
list2
中ID为“id3”的项目添加到
列表的项目“id3”中。比如合并。我不想将它们添加到列表的底部或在项目之间插入它们。我尝试了
Concat
方法,但它只是添加了项目并添加到列表的末尾:

list = list.Concat(list2).ToList();
编辑:

[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
我试着用另一种方式来解释:

我的
列表
如下所示:

[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"
[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[5] => test = "id1", test2 = "somerandomtext1"
[6] => test = "id2", test2 = "somerandomtext2"
[7] => test = "id3", test2 = "somerandomtext3"
我的
列表2
如下所示:

[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"
[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[5] => test = "id1", test2 = "somerandomtext1"
[6] => test = "id2", test2 = "somerandomtext2"
[7] => test = "id3", test2 = "somerandomtext3"
当我合并列表时,它应该是这样的:

[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
但看起来是这样的:

[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"
[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""
[5] => test = "id1", test2 = "somerandomtext1"
[6] => test = "id2", test2 = "somerandomtext2"
[7] => test = "id3", test2 = "somerandomtext3"

有什么建议吗?

因此您需要在项目级别而不是列表级别进行合并

我相信有一些聪明的方法可以使用Linq, 但是我对Linq没有太多的经验,所以我可以推荐一个简单的嵌套for循环:

foreach (GridViewSource src1 in list1)
{
    foreach(GridViewSource src2 in list2)
    {
        if(src1.test1 == src2.test1)
        {
            src1.test2 = src2.test2;
        }
    }
}

我认为您使用
列表是走错了路。为此,a更适合这种情况

然后你可以做:

dict[src.test] = src;
在第二个代码中,您需要更新(或添加)项目:


您必须使用自定义比较器:

public class MyComparer: IEqualityComparer<T>
{
    public bool Equals(T o1,T o2)
    {
        // They are the same object
        if (object.ReferenceEquals(o1, o2))
            return true;
        // They are not the same
        if (o1 == null || o2 == null)
            return false;
        // They have the same ID
        return o1.test1.Equals(o2.test1);
    }

    public int GetHashCode(X x)
    {
        return x.ID.GetHashCode();
    }
}

我没有测试过这段代码。

“我不想将它们添加到列表的底部或在项目之间插入它们。”那么您想将它们添加到哪里?这有点难以解释。我将编辑我的问题,1为什么不缩短列表?我编辑了我的问题,请看一看:)?很好,正是我需要的。谢谢:)