Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:37:41+00:00 2026-05-27T18:37:41+00:00

I have a L2E query that returns some data that contains duplicate objects. I

  • 0

I have a L2E query that returns some data that contains duplicate objects. I need to remove those duplicate objects. Basically I should assume that if their IDs are the same then the objects are duplicate. I’ve tried q.Distinct(), but that still returned duplicate objects. Then I’ve tried implementing my own IEqualityComparer and passing it to the Distinct() method. The method failed with following text:

LINQ to Entities does not recognize the method
‘System.Linq.IQueryable1[DAL.MyDOClass]
Distinct[MyDOClass](System.Linq.IQueryable
1[DAL.MyDOClass],
System.Collections.Generic.IEqualityComparer`1[DAL.MyDOClass])’
method, and this method cannot be translated into a store expression.

And here is the implementation of EqualityComparer:

  internal class MyDOClassComparer: EqualityComparer<MyDOClass>
    {
        public override bool Equals(MyDOClass x, MyDOClass y)
        {
            return x.Id == y.Id;
        }

        public override int GetHashCode(MyDOClass obj)
        {
            return obj == null ? 0 : obj.Id;
        }
    }

So how do I write my own IEqualityComparer properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:37:42+00:00Added an answer on May 27, 2026 at 6:37 pm

    An EqualityComparer is not the way to go – it can only filter your result set in memory eg:

    var objects = yourResults.ToEnumerable().Distinct(yourEqualityComparer);
    

    You can use the GroupBy method to group by IDs and the First method to let your database only retrieve a unique entry per ID eg:

    var objects = yourResults.GroupBy(o => o.Id).Select(g => g.First());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two long type columns that I want to concat during sql query.
Have a LinqtoSql query that I now want to precompile. var unorderedc = from
I have a set of tasks that I need to select distinct on (group
I'm trying to recreate some SQL queries I have into L2E statements. One of
Have some dates in my local Oracle 11g database that are in this format:
Have created simple Ajax enabled contact forms before that have around 12 fields -
I´m have a problem using EF for my data model. I have this code
Have searched the database but need to specifically sum(of hours flown or days off)in
I have the following stored procedure that I am trying to convert to a
Have an app that can use tts to read text messages. It can also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.