我有一個應用程序通過NHibernate實現。我的項目有一個Person
類:如何使用select在LINQ中關聯到NHibernate
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查詢它有一個選擇的人的一些性質。
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)
我該怎麼做?
我的'人'類非常棒。它有很多屬性和關聯。您的解決方案與SQL中的select * from tblPersons'相當。但是我需要選擇Person類的一些屬性和關聯。 – Ehsan