简体   繁体   English

Linq平等比较不起作用

[英]Linq equality comparing 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 helper: 和这个助手:

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'm getting this exception on my linq statement: 我的linq语句上出现此异常:

Unrecognised 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) 无法识别的方法调用:System.Collections.Generic.EqualityComparer`1 [[System.Guid,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]:布尔值Equals(System.Guid,System.Guid)

What does this mean? 这是什么意思? I'm not even sure how to debug this correctly. 我什至不知道如何正确调试它。 I could have sworn that the code above was working... 我可能发誓上面的代码正在工作...

You cannot do comparison in such way for linq to any database provider. 您不能以这种方式将linq与任何数据库提供程序进行比较。 Provider cannot transform this into expression tree. 提供程序无法将其转换为表达式树。 So you must use it after .ToArray() or give an Expression<Func<RegisterCardBase, bool>> into Where instead of lambda. 因此,您必须在.ToArray()之后使用它,或将Expression<Func<RegisterCardBase, bool>>放入Where而不是lambda。

PS: Why are you doing so strange actions with Guid? PS:您为什么对Guid做如此奇怪的动作? How it is storaged in database? 它如何存储在数据库中?

NHibernate's Linq provider tries to convert the Linq query to HQL and eventually to SQL. NHibernate的Linq提供程序尝试将Linq查询转换为HQL,最后转换为SQL。 The lambda expression you have in your Where method is not supported by default in NHibernate. NHibernate默认不支持Where方法中具有的lambda表达式。

However, the NHibernate Linq provider is extensible . 但是,NHibernate Linq提供程序是可扩展的 You can create your own extensions for handling various unsupported expressions. 您可以创建自己的扩展名以处理各种不受支持的表达式。

Alessandro Giorgetti has a good sample on how to extend Linq provider to support String.Equals with StringComparison option . Alessandro Giorgetti就如何扩展Linq提供程序以支持String.EqualsStringComparison选项提供了一个很好的示例。

Edit 编辑

I just realized that you are using QueryOver, and not NHibernate Linq. 我只是意识到您正在使用QueryOver,而不是NHibernate Linq。 You should probably remove tag. 您可能应该删除标记。 My answer is somewhat relevant if you switch to session.Query<Entity>() . 如果您切换到session.Query<Entity>()那么我的答案会有些相关。 Still, you might reconsider your approach for converting Id and using it in Where . 不过,您可能会重新考虑转换Id并在Where使用它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM