My question is at the end of the following code in comments:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MyObj
{
int m_id;
public MyObj(int id)
{
this.m_id = id;
}
public int GetId()
{
return this.m_id;
}
}
class Program
{
static void Main(string[] args)
{
var m0 = new MyObj(0);
var m1 = new MyObj(1);
var m2 = new MyObj(2);
var m3 = new MyObj(3);
var m4 = new MyObj(4);
List<MyObj> refList1 = new List<MyObj>()
{
m0, m1, m2, m3
};
List<MyObj> refList2 = new List<MyObj>()
{
m1, m2, m3, m4
};
//How to merge refList2 into refList1 without id repeating,
//so refList1 must be [m0, m1, m2, m3, m4]
}
}
}
You would do this using LINQ, with:
However, this requires either that
MyObjimplementsIEquatable<MyObj>, or the other overload ofUnionis used which takes anIEqualityComparer<T>parameter.The first solution would need this change:
The second solution would go like:
and would not require changes to
class MyObj.