Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Calculated Property problem

I've an Entity Framework model (v.1.0) that I'm trying to extends with a calculated property.

I've created the partial class to extend the entity object "Offer" in this way:

namespace MyModelNamespace
{
    public partial class Offer
    {
        public bool MyProperty
        {
            get 
            {
                // my stuffs the return true or false
            }
        }
    }
}

It compiles without problem in my assembly, but at runtime, when I'm trying to do something like this:

_myEntities.OfferSet.FirstOrDefault(o=>o.MyProperty);

I retrieve this error:

The number of members in the conceptual type 'MyModelNamespace.Offer' does not match with the number of members on the object side type 'MyModelNamespace.Offer'. Make sure the number of members are the same.

...any suggestions???

like image 804
tanathos Avatar asked Jul 29 '10 14:07

tanathos


2 Answers

Yes, you can do this. Use the LINQ translations library.

like image 87
Craig Stuntz Avatar answered Nov 08 '22 16:11

Craig Stuntz


You can't push custom properties down to the database. You should have gotten something like

"The specified type member 'MyProperty' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

The only valid way to do what you want is to get it into memory. And then filter.

   _myEntities.OfferSet.ToList().FirstOrDefault(o=>o.MyProperty);

I am guessing there is probably a better way to accomplish whatever you are doing.

EDIT I agree Craig there is another way, assuming * it can be translated into a store expression. If you are mapping a property to some type of query, you will be able to search/push down the filter.

like image 44
Nix Avatar answered Nov 08 '22 17:11

Nix