的LINQ to NHibernate的,"通过IDS&QUOT阵列得到;询问 [英] LINQ to NHibernate, "get by array of ids" query

查看:177
本文介绍了的LINQ to NHibernate的,"通过IDS&QUOT阵列得到;询问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code:

 public IList<T> GetByMultipleIds(int[] ids)
 {
        List<T> result =
            _session.Linq<T>()
                .Where(x => ids.Contains(x.Id)).ToList();

        return result;
 }

抛出:

An exception of type 'System.NullReferenceException' occurred in 
NHibernate.DLL but was not handled in user code

Additional information: Object reference not set to an instance of an object.

IDS = {1}; T是typeof运算(富)有正确的映射。

ids={1}; T is typeof(foo) which has correct mapping.

FOO表有预期的数据。

foo table has expected data.

FOO继承entityBase其中有一个名为标识公共虚拟道具。简单_session.Get(IDS [0])的作品。

foo inherits entityBase which has public virtual prop named Id. simple _session.Get(ids[0]) works.

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria 
subcriteria, String propertyName) +13
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetType(ICriteria 
subcriteria, String propertyName) +19
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetTypeUsingProjection
(ICriteria subcriteria, String
propertyName) +94
NHibernate.Criterion.InExpression.AssertPropertyIsNotCollection(ICriteriaQuery 
criteriaQuery, ICriteria
criteria) +19
NHibernate.Criterion.InExpression.ToSqlString(ICriteria criteria, ICriteriaQuery 
criteriaQuery, IDictionary`2 enabledFilters) +38
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition
(IDictionary`2 enabledFilters) +223
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable 
persister, CriteriaQueryTranslator
translator, ISessionFactoryImplementor factory, CriteriaImpl criteria, String 
rootEntityName, IDictionary`2 enabledFilters) +296
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, 
ISessionFactoryImplementor
factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 
enabledFilters) +131
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +173
NHibernate.Impl.CriteriaImpl.List(IList results) +41
NHibernate.Impl.CriteriaImpl.List() +35

这一次也不行:

This one does not work either:

IList<T> result =  
  (_session.Linq<T>().Where(a => new[] {1}.Contains(a.Id))).ToList();

奇怪,但这工作

IList<foo> result =  
  (_session.Linq<foo>().Where(a => new[] {1}.Contains(a.Id))).ToList();

这一个工作太:

IList<T> result =_session.Linq<T>()
  .Where(x => 1==1).ToList();

但我需要它是通用的。

But i need it to be generic.

任何想法可能是错误的?

Any ideas what might be wrong?

也许切换到NHibernate的2.1测试版将帮助?

Maybe switching to nhibernate 2.1 beta would help?

目前是这样的:

public IList<TEntity> GetByMultipleIds(int[] ids)
    {
        //TODO: somehow query whole list at once
        List<TEntity> result = new List<TEntity>();

        foreach (var id in ids) {
            int tempId = id;
            result.Add(_session.Get<TEntity>(tempId));
        }

        return result;
    }

但是,这只是一个蹩脚的补丁。 :/

But that's just a lame patch. :/


其实 - 我的同事发现使用的ICriteria一种解决方法(我会在以后添加code)
。 和允许排序ID阵列优雅的实体。

Actually - my co-worker found a workaround using ICriteria (i'll add code later).
And this allows to sort entities by id array elegantly.

推荐答案

记住NHibernate的继承你的类,不直接使用它们每它的实现的IList的。可能不是你想要听到的,但因为你的类代理,电流LINQ的实现还不能处理它正确可言,这就是问题就来什么。

Remember that NHibernate inherits your classes and doesn't use them directly per it's implementation of IList. Probably not what you want to hear but since your classes are proxied and the current Linq implementation doesn't handle it correctly at all, that's where the problem comes in.

使用代理的类中对LINQ标准的通用方法的结合只是打击在很多方面与目前的执行情况。像ShaneC在他的评论说,有他们回去,并开始充分的理由从头重新写它。

The combination of a generic method using a proxied class in criteria for Linq just blows up in so many ways with the current implementation. Like ShaneC said in his comments, there's good reason they went back and started re-writing it from scratch.

我知道你的修复之后,但遗憾的是在这种情况下,答案是等待NHibernate的2.1是完整的,或者使用一个工作周围像你正在做的现在。

I know your after a fix, but sadly the answer in this case is wait for NHibernate 2.1 to be complete or use a work-around like you are doing for now.

这篇关于的LINQ to NHibernate的,&QUOT;通过IDS&QUOT阵列得到;询问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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