How can I get distinct values using Linq to NHibernate?

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-09-09T15:09:34Z Indexed on 2012/07/02 3:16 UTC
Read the original article Hit count: 141

I've been trying to get distinct values using Linq to NHibernate and I'm failing miserably.

I've tried:

var query = from requesters in _session.Linq<Requesters>()
        orderby requesters.Requestor ascending
        select requesters;

return query.Distinct();

As well as

var query = from requesters in _session.Linq<Requesters>()
        orderby requesters.Requestor ascending
        select requesters;

return query.Distinct(new RequestorComparer());

Where RequestorComparer is

public class RequestorComparer : IEqualityComparer<Requesters>
{

    #region IEqualityComparer<Requesters> Members
    bool IEqualityComparer<Requesters>.Equals(Requesters x, Requesters y)
    {
        //return x.RequestorId.Value.Equals(y.RequestorId.Value);
        return ((x.RequestorId == y.RequestorId) && (x.Requestor == y.Requestor));
    }

    int IEqualityComparer<Requesters>.GetHashCode(Requesters obj)
    {
        return obj.RequestorId.Value.GetHashCode();
    }
    #endregion
}

No matter how I structure the syntax, it never seems to hit the .Distinct(). Without .Distinct() there are multiple duplicates by default in the table I'm querying, on order of 195 total records but there should only be 22 distinct values returned.

I'm not sure what I'm doing wrong but would greatly appreciate any assistance that can be provided.

Thanks

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about distinct