LINQ to Entities是否在"where"子句谓词中支持IEquatable? [英] Does LINQ to Entities support IEquatable in a 'where' clause predicate?

查看:73
本文介绍了LINQ to Entities是否在"where"子句谓词中支持IEquatable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的基本实体类型,称为EntityBase,它实现了IEquatable< EntityBase>.

I have a simple base entity type called EntityBase that implements IEquatable<EntityBase>.

我在LINQ到实体之外使用了此代码(具有 EF 4.1)使用谓词where子句过滤从EntityBase派生的类型的列表,该子句比较对象实例而不是对象实例从EntityBase继承的ID属性.所以基本上我想这样做:

I've used this code outside of LINQ to Entities (with EF 4.1) to filter a list of types derived from EntityBase using a predicate where clause that compares the object instances rather than the ID property they inherit from EntityBase. So basically I want to do this:

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

而不是这个:

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

不幸的是,EF 4.1,特别是LINQ to实体引发了异常:

Unfortunately, EF 4.1 and specifically LINQ to Entities throws the exception:

无法创建类型为'EFTest.UserAccount'的常量值.在这种情况下,仅支持基本类型(例如Int32,String和Guid).

Unable to create a constant value of type 'EFTest.UserAccount'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

这是否意味着我们无法在Entity Framework 4.1中执行简单的对象比较谓词,还是我的IEquatable实现在某种程度上是错误的?

Does this mean we cannot do a simple object comparison predicate in Entity Framework 4.1 or is my IEquatable implementation wrong in some way?

P.S .:这是IEquatable代码:

P.S.: here's the IEquatable code:

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
}

推荐答案

不是,不是.传递给Entity Framework linq查询的C#表达式绝不会按原样"执行,而是对表达式树进行评估并将其转换为SQL语句.

It doesn't, no. The C# expressions you pass into Entity Framework linq queries are never executed "as is", but rather the expression tree is evaluated and turned into SQL statements.

查看您的IEquatable实现,不可能实现它-Object.ReferenceEquals()GetType()不能转换为SQL语句.

Looking at your IEquatable implementation, there's no way it could possibly work - Object.ReferenceEquals() and GetType() cannot be turned into SQL statements..

这篇关于LINQ to Entities是否在"where"子句谓词中支持IEquatable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆