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.
Unfortunately you can’t use your
Equalsimplementation in a query to EF because it can’t decompile your code to see how it’s done. You should be fine usingAnymethod with a predicate expression:(I made the fields up just to show the idea,
otheris theShippingInformationyou’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:
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>>).