Linq equality not working

Given this method:

internal static IEnumerable<Entity> GetByParentImpl<Entity, TKey>(this ICanGetByParent<Entity, TKey> self, TKey parentId, string fieldName) 
    where Entity : class 
{
    RepositoryBase<Entity> rb = (RepositoryBase<Entity>)self;
    rb.unitOfWork.Begin();

    var entities = rb.unitOfWork.Session.QueryOver<Entity>()
        .Where(e => EqualityComparer<TKey>.Default.Equals(GetId<Entity, TKey>(e, fieldName), parentId))
        .List();

    return entities;
}

And this assistant:

private static TKey GetId<Entity, TKey>(object obj, string fieldName) where Entity : class
{
    TKey id = default(TKey);

    switch(id.GetType().Name) {
        case "Int32":
            break;
        case "Guid":
            id = (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString((string)typeof(Entity).GetField(fieldName).GetValue(obj));
            break;
    }

    return id;
}

I get this exception in my linq expression:

Unrecognized method call: System.Collections.Generic.EqualityComparer`1 [[System.Guid, mscorlib, Version = 4.0.0.0, Culture = Neutral, PublicKeyToken = b77a5c561934e089]]: Boolean Equals (System.Guid, System.Guid)

What does it mean? I'm not even sure how to properly debug this. I could swear the code above works ...

+5
source share
2 answers

You cannot compare this way for linq with any database provider. The provider cannot convert this to an expression tree. Therefore, you should use it after .ToArray () or give Expression<Func<RegisterCardBase, bool>>in Where instead of lambda.

PS: Guid? ?

+3

NHibernate Linq Linq HQL SQL. , Where, NHibernate.

NHibernate Linq . .

, Linq String.Equals StringComparison.

Edit

, QueryOver, NHibernate Linq. , linq. , session.Query<Entity>(). , Id Where.

+2

All Articles