I currently have 2 queries that are returning lists of MyModel like this:
var q1 = ....
select new MyModel()
{
TheData1 = ...
I think frenchie wants a list of MyModel back instead of just the TheUniqueID.
You need to create a MyModelTheUniqueIDComparer class and pass a new instance of it as a second argument into Union:
class MyModelTheUniqueIDComparer : IEqualityComparer
{
public bool Equals(MyModel x, MyModel y)
{
return x.TheUniqueID == y.TheUniqueID;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(MyModel myModel)
{
return myModel.TheUniqueID.GetHashCode();
}
}
Then you can call to get the result:
var result = q1.Union(q2, new MyModelTheUniqueIDComparer());
See http://msdn.microsoft.com/en-us/library/bb358407.aspx for a more details.
Update:
Try this:
public class A
{
public string TheData1 { get; set; }
public string TheData2 { get; set; }
public string UniqueID { get; set; }
}
public class AComparer : IEqualityComparer
{
#region IEqualityComparer Members
public bool Equals(A x, A y)
{
return x.UniqueID == y.UniqueID;
}
public int GetHashCode(A obj)
{
return obj.UniqueID.GetHashCode();
}
#endregion
}
And test with this:
var listOfA = new List();
var q1 = from a in listOfA
select new A()
{
TheData1 = "TestData",
TheData2 = "TestData",
UniqueID = a.UniqueID
};
var anotherListOfA = new List();
var q2 = from a in anotherListOfA
select new A()
{
TheData1 = "TestData",
TheData2 = "TestData",
UniqueID = a.UniqueID
};
q1.Union(q2, new AComparer());
Make sure you have using System.Linq;