How to pick a specific object from a Linq Query using Distinct?

Posted by Holli on Stack Overflow See other posts from Stack Overflow or by Holli
Published on 2010-05-19T11:43:10Z Indexed on 2010/05/19 11:50 UTC
Read the original article Hit count: 187

Filed under:
|

I have a list with two or more objects of class Agent.

Name = "A"

Priority = 0

ResultCount = 100

;

Name = "B"

Priority = 1

ResultCount = 100

;

Both objects have the same ResultCount. In that case I only need one object and not two or more. I did this with a Linq Query with Distinct and an custom made Comparer.

IEnumerable<Agent> distinctResultsAgents = 
(from agt in distinctUrlsAgents select agt).Distinct(comparerResultsCount);

With this query I get only one object from the list but I never know which one. But I don't want just any object, I want object "B" because the Priority is higher then object "A". How can I do that?

My custom Comparer is very simple and has a method like this:

    public bool Equals(Agent x, Agent y)
    {
        if (x == null || y == null)
            return false;

        if (x.ResultCount == y.ResultCount)
            return true;

        return false;
    } 

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ