我有一个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'.
堆栈跟踪是:
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)
我怎样才能做到这一点?