2

I have a generic list filled with items. In this list I have one column which is unique (like an ID). And I have another generic list with other items and the same ID column. This is the way I fill the items to the lists:

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);
}

What I'm trying to do is to add items from list2 to list if they have the same ID. So it should add an item from list2 with the ID "id3" to the item "id3" of list. Like merging. I don't want to add them to the bottom of the list or insert them between the items. I tried Concat method but it's just adding the items add the end of the list:

list = list.Concat(list2).ToList();

EDIT:

I try to explain it another way:

My list looks like this:

[0] => test = "id1", test2 = ""
[1] => test = "id2", test2 = ""
[2] => test = "id3", test2 = ""
[3] => test = "id4", test2 = ""
[4] => test = "id5", test2 = ""

And my list2 looks like this:

[0] => test = "id1", test2 = "somerandomtext1"
[1] => test = "id2", test2 = "somerandomtext2"
[2] => test = "id3", test2 = "somerandomtext3"

And when I merge the lists, it should look like this:

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

But it looks like this:

[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"

Any suggestions?

6
  • " I don't want to add them to the bottom of the list or insert them between the items." Then where do you want to add them? Commented Jul 14, 2015 at 7:54
  • It's a bit difficult to explain. I'll edit my question, 1sec Commented Jul 14, 2015 at 7:55
  • Why dont you short the list? Commented Jul 14, 2015 at 7:57
  • I edited my question, please have a look :) Commented Jul 14, 2015 at 8:01
  • stackoverflow.com/questions/15523097/… ? Commented Jul 14, 2015 at 8:03

3 Answers 3

3

So you need to merge on the item level, not on the list level.

I'm sure there is some clever way to do it using Linq, but I don't have too much experience with Linq, so I can suggest a simple nested for loops:

foreach (GridViewSource src1 in list1)
{
    foreach(GridViewSource src2 in list2)
    {
        if(src1.test1 == src2.test1)
        {
            src1.test2 = src2.test2;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You'll have to use a custom comparer:

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();
    }
}

And then use it in the call:

list = list.Union(list2, new MyComparer()).ToList();

I have not tested this code.

Comments

0

I think you are on the wrong path using a List<T> for this, a Dictionary<K, V> is better suited for this situation.

Then you could do:

dict[src.test] = src;

And in the second code, where you need to update (or add) an item:

GridViewSource outsrc;
if (dict.TryGetValue(src.test, out outsrc))
{
    // items exists: update
    outsrc.test2 = src.test2;
}
else
{
    // item doesn't exist: add
    dict[src.test] = src;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.