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 6904841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:06:40+00:00 2026-05-27T08:06:40+00:00

I am having troubles comparing objects In entity Framework 4.0. After googling for a

  • 0

I am having troubles comparing objects In entity Framework 4.0. After googling for a while I found a blog post in 2008 which stated what my problem was and why it is occuring.Blog post describing my problem in depth.

To sum up the blog post you cannot do a custom object comparison with the EF framework at all. For example

public Foo
{
public int ID{get;set;}
public string Name {get;set;}
//I overrode the .Equals AND the == operator
}

public getFoo(Foo target)
{
   DC.foos.FirstOrDefault(x => x == target);
}

System.NotSupportedException: Unable to create a constant value of type ‘Foo’ Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.

This is by design according to MicroSoft.

Can someone point me in a direction to find if this sort of Object comparison is supported if I do some magic interface or magic overload? thank you very much!

  • 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-27T08:06:41+00:00Added an answer on May 27, 2026 at 8:06 am

    Since EF needs to translate your LINQ statements to SQL statements, you can’t do this. If you have complex comparison logic in your overridden Equals() method you will have to duplicate that in the LINQ statement.

    Since LINQ uses deferred execution you could probably encapsulate that logic in a method that returns an IQueryable<T> you can incorporate elsewhere.

    Here’s an example:

    public IQueryable<Foo> FoosEqualTo(IQueryable<Foo> allFoos, Foo target) {
        return from foo in allFoos
               where foo.Id == target.Id // or other comparison logic...
               select foo;
    }
    
    public Foo getFoo(Foo target) {
        return FoosEqualTo(DC.foos, target).FirstOrDefault();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble comparing values found in VARCHAR fields. I have a table
I'm having trouble comparing strings in C (with which I am fairly new to).
I don't know much about multimedia development and am having some trouble comparing Silverlight
I'm having trouble composing a CakePHP find() which returns the records I'm looking for.
I'm having troubles with HttpWebRequest/HttpWebResponse and cookies/CookieContainer/CookieCollection. The thing is, if the web server
I am having troubles migrating from OC4J 10.1.2.3 to 10.1.3.1.4. The problem is for
I'm having troubles with the order of my code in my program. right now
I'm having troubles using Blend with my visual studio solution. In Runtime, and compile
im currently having troubles on my codes in C#. I want to split strings
I am having troubles with jQuery's load function and am hoping for some help.

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.