Linq平等比较不起作用

Tyl*_*ght 5 c# nhibernate

鉴于此方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

这个助手:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的linq语句中出现了这个异常:

无法识别的方法调用:System.Collections.Generic.EqualityComparer`1 [[System.Guid,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]:Boolean Equals(System.Guid,System.Guid)

这是什么意思?我甚至不确定如何正确调试.我可以发誓上面的代码正在工作......

Kir*_*nov 3

您不能以这种方式对 linq 与任何数据库提供程序进行比较。提供者无法将其转换为表达式树。因此,您必须在 .ToArray() 之后使用它,或者给出一个 Expression<Func<RegisterCardBase, bool>>intoWhere 而不是 lambda。

PS:你为什么用 Guid 做这么奇怪的动作?它是如何存储在数据库中的?