如何在linq中使用select对NHibernate的关联

时间:2012-11-13 05:39:57

标签: c# linq nhibernate select linq-to-nhibernate

我有 NHibernate 实施的应用程序。我的项目有一个Person类:

public class Person : RootEntityBase
{    
    virtual public string FirstName { get; set; }
    virtual public string LastName { get; set; }
    virtual public string FatherName { get; set; }

    virtual public IList<Asset> Assets { get; set; }
    virtual public IList<PersonPicture> PersonPictures { get; set; }
}

通过这种映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain.Entities">
  <class name="Person" table="Person_Person" >

    <id name="Id">
      <generator class="native" />
    </id>

    <property name="FirstName" not-null="true"/>

    <property name="LastName" not-null="true"/>

    <property name="FatherName" not-null="true"/>

    <bag name="Assets" inverse="true" table="Person_Asset" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="Asset"/>
    </bag>

    <bag name="PersonPictures" inverse="true" table="Person_PersonPicture" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="PersonPicture"/>
    </bag>

  </class>

我需要linq的查询,它有一个选择Person的一些属性。

var q = SessionInstance.Query<Person>()
               .Select(x => new Person(x.Id)
                                  {
                                      FirstName = x.FirstName,
                                      LastName = x.LastName,
                                      PersonPictures = x.PersonPictures //this line has error
                                  })
               .ToList();

通过添加PersonPictures来选择,此查询通过此消息具有运行时异常:

could not execute query\r\n[ select person0_.Id as col_0_0_, person0_.FirstName as col_1_0_, person0_.LastName as col_2_0_, ...

内部异常的消息是:

Incorrect syntax near the keyword 'as'.

Stack Trace是:

   at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
   at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)
   at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
   at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
   at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
   at NHibernate.Impl.ExpressionQueryImpl.List()
   at NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

我该怎么做?

3 个答案:

答案 0 :(得分:0)

我不是Linq To nHibernate ninja,但是下面呢?

var q = SessionInstance.Query<Person>().Fetch(p => p.PersonPictures).ToList();

你需要做那个投影吗?我在答案中省略了它,但如果需要可以添加它。

参考文献:

Linq for NHibernate and fetch mode of eager loading

Fetch vs FetchMany in NHibernate Linq provider

http://mikehadlow.blogspot.co.nz/2010/08/nhibernate-linq-eager-fetching.html

答案 1 :(得分:0)

您的映射是正确的。使用Criteria API时,我可以按预期获得结果。

但您所引用的问题已知并报告: https://nhibernate.jira.com/browse/NH-2707

但我害怕,不是固定的。 那么,如果可以(在这种情况下)使用Criteria:

// example returning the complete list
var query = session.CreateCriteria<Preson>().List<Person>(); 

然后你可以继续:

query.Select(x => new Person(x.Id)
...

答案 2 :(得分:0)

我无法理解您为什么要尝试进行以下查询:

var q = SessionInstance.Query<Person>()
           .Select(x => new Person(x.Id)
                              {
                                  FirstName = x.FirstName,
                                  LastName = x.LastName,
                                  PersonPictures = x.PersonPictures //this line has error
                              })
           .ToList();

映射和域类看起来正确。所以你应该继续以下几点:

IList<Person> persons = (from p in SessionInstance.Query<Person>()
select p).ToList();

如果要对图片应用过滤器:

IList<Person> persons = (from p in SessionInstance.Query<Person>()
select p
where p.PersonPictures.Any(pp => pp.Property == [...]).ToList();

无需重新映射每个属性,这是NHibernate映射+ NHibernate LINQ提供程序的工作。