Well, here's a very interesting way if you're using .NET 3.5 using LINQ.
First, I added a comparer on my "Custom" class for the collection in Question.
Code:
namespace BusinessObjects
{
public partial class EmployeesCollection : esEmployeesCollection, IEqualityComparer
{
#region IEqualityComparer Members
bool IEqualityComparer.Equals(Employees x, Employees y)
{
return x.EmployeeID == y.EmployeeID;
}
int IEqualityComparer.GetHashCode(Employees obj)
{
return obj.EmployeeID.Value;
}
#endregion
}
}
Now all I need to do is use the Union operator like this:
Code:
// Two sets of EmployeeID's
List<int> one = new List<int> { 1, 2, 3, 4 };
List<int> two = new List<int> { 3, 4, 5, 6 };
// Load the 1st one
EmployeesCollection oneColl = new EmployeesCollection();
oneColl.Query.Where(oneColl.Query.EmployeeID.In(one));
oneColl.Query.Load();
// Load the 2nd one
EmployeesCollection twoColl = new EmployeesCollection();
twoColl.Query.Where(twoColl.Query.EmployeeID.In(two));
twoColl.Query.Load();
// Use LINQ to return a Union (intersection),
//
// NOTICE THAT THE SECOND PARAMETER TO UNION() METHOD IS THE
// COMPARER CLASS WHICH COULD BE ANY INSTANTIATED EmployeesCollection
IEnumerable uniqueQuery =
oneColl.Union(twoColl, twoColl).OrderBy(e => e.EmployeeID);
// Did it work? Yes, it prints 1,2,3,4,5,6
foreach (Employees emp in uniqueQuery)
{
Console.Write(emp.EmployeeID.Value.ToString());
}
// At this point I could create another collection and use Attach
// to create an entirely new collection
EmployeesCollection three = new EmployeesCollection();
foreach (Employees emp in uniqueQuery)
{
three.AttachEntity(emp);
}
The alternative in .NET 2.0 would be to do something like this:
Code:
// Two sets of EmployeeID's
List<int> one = new List<int> { 1, 2, 3, 4 };
List<int> two = new List<int> { 3, 4, 5, 6 };
// Load the 1st one
EmployeesCollection oneColl = new EmployeesCollection();
oneColl.Query.Where(oneColl.Query.EmployeeID.In(one));
oneColl.Query.Load();
// Load the 2nd one
EmployeesCollection twoColl = new EmployeesCollection();
twoColl.Query.Where(twoColl.Query.EmployeeID.In(two));
twoColl.Query.Load();
foreach (Employees emp in twoColl)
{
if (null == oneColl.FindByPrimaryKey(emp.EmployeeID.Value))
{
oneColl.AttachEntity(emp);
}
}
foreach (Employees emp in oneColl)
{
Console.Write(emp.EmployeeID.Value.ToString());
}
Hope those ideas help ...
EntitySpaces |
Twitter |
BLOG | Please honor our Software License