none
Distinct RRS feed

  • Question

  • I have one select statement like:

    var data = (from c in context.customers

                    join m in context.contacts on m.customerId equals c.customerid

                    ).Distinct();

    The above select statement is returning some duplicate rows. I have used Distinct to get only the unique rows. But still getting the duplicate rows. How to get the unique rows?

    Thanks in advance

     

    Saturday, October 18, 2008 5:06 AM

Answers

  • Hi

    here is a example of distinct
    List<int>
    numbers = new List<int> {1, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10};
    IEnumerable<int> val = numbers.Distinct();
    foreach (int num in val)
    Console.WriteLine(num)
    The results are
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    but as i know for your case you should make a custom EqualityComparer class.
    here i do one:

     public class MyComparer : IEqualityComparer<
    Customer>
        {
            public bool Equals(Customer x,
    Customer y)
            {
                return (x.FirstName == y.
    FirstName );
                 
            }

            public int GetHashCode(Customer obj)
            {
                return obj.ToString().GetHashCode();
            }
        }


    IEnumerable<Customer> CustomerDistinctList = db.Customers.ToList().Distinct(new
    MyComparer ());

    I hope this was helpfull.
    Saturday, October 18, 2008 7:46 AM
    Answerer

All replies

  • Hi

    here is a example of distinct
    List<int>
    numbers = new List<int> {1, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10};
    IEnumerable<int> val = numbers.Distinct();
    foreach (int num in val)
    Console.WriteLine(num)
    The results are
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    but as i know for your case you should make a custom EqualityComparer class.
    here i do one:

     public class MyComparer : IEqualityComparer<
    Customer>
        {
            public bool Equals(Customer x,
    Customer y)
            {
                return (x.FirstName == y.
    FirstName );
                 
            }

            public int GetHashCode(Customer obj)
            {
                return obj.ToString().GetHashCode();
            }
        }


    IEnumerable<Customer> CustomerDistinctList = db.Customers.ToList().Distinct(new
    MyComparer ());

    I hope this was helpfull.
    Saturday, October 18, 2008 7:46 AM
    Answerer
  • I was dealing with a similar issue and your post helped me track down the problem.  Thanks!

     

    Wednesday, November 5, 2008 8:57 PM
  • I've followed the code example from above but Distinct() is not calling the Equals method in the Comparer class I created.

    What could I be doing wrong?

        public class DeductibleComparer : IEqualityComparer<DeductibleHp>
        {
            #region IEqualityComparer<DeductibleHp> Members

            public bool Equals(DeductibleHp x, DeductibleHp y)
            {
                bool Result = false;

                if (x.Amount.Equals(y.Amount) && x.StartDate.Date.Equals(y.StartDate.Date) && x.Rate.Equals(y.Rate))
                {
                    Result = true;
                }
                //if (x.HpAmt.Equals(y.HpAmt) && x.HpBegDate.Equals(y.HpBegDate) && x.HpRate.Equals(y.HpRate))
                //{
                //    Result = true;
                //}

                return Result;
            }

            public int GetHashCode(DeductibleHp obj)
            {
                return obj.ToString().GetHashCode();
            }

            #endregion
        }

                HP3000.DeductibleHp Ded1;
                HP3000.DeductibleHp Ded2;
                HP3000.DeductibleHp Ded3;
                List<HP3000.DeductibleHp> TestDistinct = new List<HP3000.DeductibleHp>();

                Ded1 = new HP3000.DeductibleHp();
                Ded1.HpAmt = 1000;
                Ded1.HpBegDate = "20090101";
                Ded1.HpRate = 55;
                TestDistinct.Add(Ded1);

                Ded2 = new HP3000.DeductibleHp();
                Ded2.HpAmt = 2000;
                Ded2.HpBegDate = "20090101";
                Ded2.HpRate = 55;
                TestDistinct.Add(Ded2);

                Ded3 = new HP3000.DeductibleHp();
                Ded3.HpAmt = 1000;
                Ded3.HpBegDate = "20090101";
                Ded3.HpRate = 55;
                TestDistinct.Add(Ded3);

                var TestQuery = TestDistinct.Distinct(new HP3000.DeductibleComparer());

    I put a break point in the Equals method and the debugger never stops.

    Regards, Mike DePouw
    Thursday, May 7, 2009 8:35 PM
  • The problem is in return obj.ToString().GetHashCode();
    You must provide equal hash code for equal values that you're wanna distinct.
    Thursday, December 3, 2009 10:45 AM