The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

remove duplicates in a collection

Last post 07-31-2008, 6:53 PM by Mike.Griffin. 1 replies.
Sort Posts: Previous Next
  •  07-31-2008, 3:34 PM 10465

    remove duplicates in a collection

    I’m combining collection and would like to know the best method to remove duplicates.   I’m not going back to the database so distinct = true is not going to work for me.

     

    Jeff

  •  07-31-2008, 6:53 PM 10470 in reply to 10465

    Re: remove duplicates in a collection

    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
View as RSS news feed in XML