C# 如何在linq到NHibernate中使用select for关联
我有一个由NHibernate实现的应用程序。我的项目有一个C# 如何在linq到NHibernate中使用select for关联,c#,linq,nhibernate,select,linq-to-nhibernate,C#,Linq,Nhibernate,Select,Linq To Nhibernate,我有一个由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 ILi
人
类:
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; }
}
内部异常的消息是:
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)
我该怎么做呢?我不是nHibernate忍者的灵丹妙药,但是下面呢
var q = SessionInstance.Query<Person>().Fetch(p => p.PersonPictures).ToList();
var q=SessionInstance.Query().Fetch(p=>p.PersonPictures.ToList();
你需要做那个投影吗?我把它从我的回答中删去了,不过如果需要的话可以加进去
参考资料:
您的映射是正确的。当使用标准API时,我可以达到预期的结果 但您所引用的问题是已知的,并已报告: 但我恐怕还没有确定。 因此,如果您可以(在此场景中)使用标准:
// example returning the complete list
var query = session.CreateCriteria<Preson>().List<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();
var q=SessionInstance.Query()
.选择(x=>newperson(x.Id)
{
FirstName=x.FirstName,
LastName=x.LastName,
PersonPictures=x.PersonPictures//此行有错误
})
.ToList();
映射和域类看起来是正确的。因此,您只需执行以下操作:
IList<Person> persons = (from p in SessionInstance.Query<Person>()
select p).ToList();
IList persons=(来自SessionInstance.Query()中的p)
选择p.ToList();
如果要对图片应用过滤器,请执行以下操作:
IList<Person> persons = (from p in SessionInstance.Query<Person>()
select p
where p.PersonPictures.Any(pp => pp.Property == [...]).ToList();
IList persons=(来自SessionInstance.Query()中的p)
选择p
其中p.PersonPictures.Any(pp=>pp.Property==[…]).ToList();
无需重新映射每个属性,这是NHibernate mapping+NHibernate LINQ提供程序的工作。My
Person
类非常棒。它有许多属性和关联。您的解决方案与SQL中tblPersons中的select*等效。但我需要选择Person类的一些属性和关联。
query.Select(x => new Person(x.Id)
...
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();