2012-11-13 51 views
2

我有一個應用程序通過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) 

我該怎麼做?

回答

0

你的映射是正確的。在使用Criteria API時,我可以達到預期的結果。

但你引用的問題是已知的,報道說: https://nhibernate.jira.com/browse/NH-2707

但我很害怕,不固定。 所以,如果你可以(在這種情況下)的使用標準:

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

然後你就可以繼續:

query.Select(x => new Person(x.Id) 
... 
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提供程序的工作。

+0

我的'人'類非常棒。它有很多屬性和關聯。您的解決方案與SQL中的select * from tblPersons'相當。但是我需要選擇Person類的一些屬性和關聯。 – Ehsan