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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:02:49+00:00 2026-05-24T07:02:49+00:00

I have a DbSet object DbSet<ShippingInformation> ShippingInformations; I have also overridden the equals operator

  • 0

I have a DbSet object DbSet<ShippingInformation> ShippingInformations; I have also overridden the equals operator for ShippingInformation.

How can I find if there is an existing object, y in the set ShippingInformations that is equal to object x, both of type ShippingInformation.

So far I have tried:

storeDB.ShippingInformations.Contains(shippingInformation);

However, that only works for primitive types.

  • 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-24T07:02:49+00:00Added an answer on May 24, 2026 at 7:02 am

    Unfortunately you can’t use your Equals implementation in a query to EF because it can’t decompile your code to see how it’s done. You should be fine using Any method with a predicate expression:

    bool exists = storeDB.ShippingInformations
        .Any(info =>
                info.CustomerID == other.CustomerID
                && info.CountryID == other.CountryID
            );
    

    (I made the fields up just to show the idea, other is the ShippingInformation you’re looking for.)

    If there are many places where you want to re-use this expression, you might want to use LinqKit to combine expressions:

    private static Expression<Func<ShippingInformation, ShippingInformation, bool>>
        isEqualExpr =
            (info, other) =>
                info.CustomerID == other.CustomerID
                && info.CountryID == other.CountryID;
    
    
    // somewhere down this class
    
    var expr = isEqualExpr; // reference the expression locally (required for LinqKit)
    bool exists = storeDB.ShippingInformations
                      .Any(x => expr.Invoke(x, other)); // "injects" equality expression
    

    Such code should be placed in data layer.

    I’m not 100% sure if the above code works though. It may very well be that EF won’t allow “other” object to be used in the query expression. If this is the case (please let me know), you’ll have to modify the expression to accept all primitive type values for comparison (in our example, it would’ve become Expression<Func<ShippingInformation, int, int, bool>>).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this DBSet public DbSet<Category> Categories { get; set; } and
I have the following DbContext , where each DbSet marked with a // This
I have a DbContext with several of the following type of members: public DbSet<JobLevel>
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have a matrix report now that has Position, Hours and Wages for a location
Have you ever obfuscated your code before? Are there ever legitimate reasons to do
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I have a couple of classes (for this example anyway) that use code first
Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework
I have a project where i use EF 4.1. At Data Context: public DbSet<Customer>

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.