locked
Not retrieving distinct records while querying all Contacts with a Resolved Case using Linq RRS feed

  • Question

  • What am I doing wrong? Not retrieving distinct records while querying all Contacts with a Resolved Case using Linq.

    Also, when uncomment “where i.StateCode.Equals(1)” I get zero results.


    Tuesday, January 14, 2014 1:00 AM

Answers

  • The distinct overload with no parameters uses the default equality comparer. For an anonymous type, the default equality comparer compares based on all public properties. In your case both the ContactId and the TicketNumber are used to determine if the results are distinct.

    In your screenshot the results show the same ContactId each time, but a different TicketNumber - so they are distinct.

    If you only 1 result per contact then you have 2 options:

    1. Do not include the TicketNumber in the results.
    2. Use a custom equality comparer that ignores the TicketNumber. This will give you each contact once, with the TicketNumber from one of the resolved cases linked to that contact. See this article on MSDN for using Distinct with a custom equality comparer.

    • Marked as answer by Allen Campbell Saturday, January 18, 2014 12:18 PM
    Tuesday, January 14, 2014 9:58 PM