LINQ to Entities supports IEquatable in the where? Where?

I have a simple base type of an EntityBase object that implements IEquatable <EntityBase>.

I used this code outside LINQ to Entities (with EF 4.1) to filter out the list of types obtained from EntityBase using the predicate where clause, which compares the object instances, and not the ID property that they inherit from EntityBase. So basically I want to do this:

UserAccount account = GetMyNewAccount();
Accounts.Where(item => item == account).SingleOrDefault();

instead of this:

UserAccount account = GetMyNewAccount();
Accounts.Where(item => item.Id == account.Id).SingleOrDefault();

Unfortunately, EF 4.1 and, in particular, LINQ to Entities throw an exception:

It is not possible to create a constant value of type "EFTest.UserAccount". In this context, only primitive types (such as Int32, String, and Guid) are supported.

, Entity Framework 4.1 IEquatable?

P.S.: IEquatable:

public class EntityBase : IEntityBase, IEquatable<EntityBase>
{
    public int Id { get; protected set; }

    #region IEquatable<EntityBase> Members

    public override bool Equals(object obj)
    {
        return Equals(obj as EntityBase);
    }

    public bool Equals(EntityBase other)
    {
        if (ReferenceEquals(other, null))
        {
            return false;
        }

        if (ReferenceEquals(this, other))
        {
            return true;
        }

        if (GetType() != other.GetType())
        {
            return false;
        }

        return Id.Equals(other.Id);
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }

    public static bool operator ==(EntityBase a, EntityBase b)
    {
        if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
        {
            return true;
        }

        if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
        {
            return false;
        }

        return a.Equals(b);
    }

    public static bool operator !=(EntityBase a, EntityBase b)
    {
        return !(a == b);
    }

    #endregion
}
+3
1

, . #, linq- Entity Framework, " ", SQL-.

IEquatable, - Object.ReferenceEquals() GetType() SQL-..

+2

All Articles