Linq to sql 带有泛型的Linq Where子句不重新确认等于基类型

Linq to sql 带有泛型的Linq Where子句不重新确认等于基类型,linq-to-sql,entity-framework,c#-4.0,Linq To Sql,Entity Framework,C# 4.0,我有以下简单的功能: public class BaseEntityRepository<TEntity, TId> : IBaseEntityRepository<TEntity, TId> where TEntity : class, TBaseEntity, IIdentifiedEntity<TId> where TId : struct { //... public virtual TEntity GetById(TId id

我有以下简单的功能:

public class BaseEntityRepository<TEntity, TId> : IBaseEntityRepository<TEntity, TId>
    where TEntity : class, TBaseEntity, IIdentifiedEntity<TId>
    where TId : struct {
//...
    public virtual TEntity GetById(TId id) {
        return (from e in GetAll() where e.Id.Equals(id) select e).SingleOrDefault();
    }
//...
}
公共类BaseEntityRepository:IBaseEntityRepository
式中tenty:类、TBaseEntity、标识身份
其中TId:struct{
//...
公共虚拟用户id(TId id){
返回(从GetAll()中的e开始,其中e.Id.Equals(Id)选择e);
}
//...
}
由于TId是通用的,我收到以下消息:

“无法创建“System.Object”类型的常量值。在此上下文中仅支持基元类型('Int32、String和Guid')。

不管它代表什么类型。我试过“Byte”,“Int16”,“Int32”,“Long”。。。信息是一样的。 我认为将泛型约束定义为struct就足以将类型重新定义为原语

顺便说一句。。。GetAll()返回一个
IQueryable

无论如何。。。有人知道解决办法吗?
谢谢

只是一个猜测。如果你试试这个怎么办

e.Id.GetHashCode() == id.GetHashCode()
那么eQuatable呢

where TId : IEquatable<TId>
其中TId:i可满足

您编写的
GetAll
返回
IQueryable
。这也是它的声明方式吗(也许这就是你的意思)?没有一种简单(任何?)的方法来约束一个基元类型;也要考虑<代码>字符串是一个原语,但不是<代码>结构> <代码>。最后一个约束是:公共类BaseEntityRepository:IBaseEntityRepository,其中tenty:class、TBaseEntity、IIIdentifiedEntity,其中TId:struct、IEquatable{我不相信哈希代码的比较可以保证两个Id的身份,因为Id可以是任何东西。整数值可以,但我不确定GUID或字符串。