linq subquery

230 views
Skip to first unread message

Aaron Fischer

unread,
Sep 15, 2010, 4:43:26 PM9/15/10
to nhusers
Is this possible with the current nhibernate 3 linq provider

var temp = from c in customers
from o in c.Orders
where
o.Price > 10
select
c.Name

Sujith

unread,
Sep 16, 2010, 2:20:34 AM9/16/10
to nhusers
I think you require a link between customers and orders

Mohamed Meligy

unread,
Sep 16, 2010, 3:41:19 AM9/16/10
to nhu...@googlegroups.com

There is a link already. He got orders from customer (from o in c.Orders, c is got by from c in customers).

 

I don't think you can, but give it a try anyway.

 

IF it doesn't work maybe you can do it the other way around. Query on orders and then select o.Customer.

 

Mohamed Meligy
Readify | Senior Developer

M:+61 451 835006 | W: www.readify.net

Description: Description: Description: Description: rss_16  Description: Description: Description: Description: cid:image003.png@01CAF81D.6A076510  Description: Description: Description: Description: cid:image005.png@01CAF81D.6A076510




--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Tim Hoolihan - @thoolihan

unread,
Sep 16, 2010, 2:16:01 PM9/16/10
to nhusers
if you want names of customers with orders that have a price > 10,
try:

var temp = from c in customers
where c.Orders.Count(o => o.Price > 10) > 0
select c.Name

Aaron Fischer

unread,
Sep 16, 2010, 2:53:40 PM9/16/10
to nhusers
That seems to work the query I am having issues with is a little more
complex.

var leads = from folder in _Session.Query<LoanFolder>()
from loan in folder.LoanFiles
from brw in loan.Application.Borrowers
select
brw.ContactDetails;

with this query I am getting a the following error:
---------------------------------------------------------------------
Specified method is not supported.
System.NotSupportedException
Void
VisitAdditionalFromClause(Remotion.Data.Linq.Clauses.AdditionalFromClause,
Remotion.Data.Linq.QueryModel, Int32)
Void Accept(Remotion.Data.Linq.IQueryModelVisitor,
Remotion.Data.Linq.QueryModel, Int32)
Void
VisitBodyClauses(Remotion.Data.Linq.Collections.ObservableCollection`1[Remotion.Data.Linq.Clauses.IBodyClause],
Remotion.Data.Linq.QueryModel)
Void VisitQueryModel(Remotion.Data.Linq.QueryModel)
Void Visit()
NHibernate.Linq.ExpressionToHqlTranslationResults
GenerateHqlQuery(Remotion.Data.Linq.QueryModel,
NHibernate.Linq.Visitors.VisitorParameters, Boolean)
NHibernate.Hql.Ast.ANTLR.Tree.IASTNode
Translate(NHibernate.Engine.ISessionFactoryImplementor)
NHibernate.Hql.IQueryTranslator[]
CreateQueryTranslators(System.String, NHibernate.IQueryExpression,
System.String, Boolean,
System.Collections.Generic.IDictionary`2[System.String,NHibernate.IFilter],
NHibernate.Engine.ISessionFactoryImplementor)
NHibernate.Hql.IQueryTranslator[] CreateTranslators(System.String,
NHibernate.IQueryExpression, System.String, Boolean,
System.Collections.Generic.IDictionary`2[System.String,NHibernate.IFilter],
NHibernate.Engine.ISessionFactoryImplementor)
Void .ctor(System.String, NHibernate.IQueryExpression, System.String,
Boolean,
System.Collections.Generic.IDictionary`2[System.String,NHibernate.IFilter],
NHibernate.Engine.ISessionFactoryImplementor)
Void .ctor(System.String, NHibernate.IQueryExpression, Boolean,
System.Collections.Generic.IDictionary`2[System.String,NHibernate.IFilter],
NHibernate.Engine.ISessionFactoryImplementor)
NHibernate.Engine.Query.IQueryExpressionPlan
GetHQLQueryPlan(NHibernate.IQueryExpression, Boolean,
System.Collections.Generic.IDictionary`2[System.String,NHibernate.IFilter])
NHibernate.Engine.Query.IQueryExpressionPlan
GetHQLQueryPlan(NHibernate.IQueryExpression, Boolean)
NHibernate.IQuery CreateQuery(NHibernate.IQueryExpression)
System.Object Execute(System.Linq.Expressions.Expression)
TResult Execute[TResult](System.Linq.Expressions.Expression)
System.Collections.Generic.IEnumerator`1[T] GetEnumerator()
Void ForEach[T](System.Collections.Generic.IEnumerable`1[T],
System.Action`1[T])

Aaron Fischer

unread,
Sep 16, 2010, 2:52:34 PM9/16/10
to nhusers
That seems to work the query I am having issues with is a little more
complex

Stefan Wenig

unread,
Sep 17, 2010, 3:52:56 AM9/17/10
to nhusers
Please post a stack trace that doesn't omit the types of methods on
the stack (how did you create this?)
Thanks,
Stefan

On Sep 16, 8:52 pm, Aaron Fischer <pretzelfi...@gmail.com> wrote:
> That seems to work the query I am having issues with is a little more
> complex
>
> var leads = from folder in _Session.Query<LoanFolder>()
> from loan in folder.LoanFiles
> from brw in loan.Application.Borrowers
> select
> brw.ContactDetails;
>
> with this query I am getting a the following error:
> ---------------------------------------------------------------------
> Specified method is not supported.
> System.NotSupportedException
> Void
> VisitAdditionalFromClause(Remotion.Data.Linq.Clauses.AdditionalFromClause,
> Remotion.Data.Linq.QueryModel, Int32)
> Void Accept(Remotion.Data.Linq.IQueryModelVisitor,
> Remotion.Data.Linq.QueryModel, Int32)
> Void
> VisitBodyClauses(Remotion.Data.Linq.Collections.ObservableCollection`1[Remo­tion.Data.Linq.Clauses.IBodyClause],

Aaron Fischer

unread,
Sep 17, 2010, 10:24:51 AM9/17/10
to nhusers
Sorry about that, I garbed the error message with out thinking.
-----------------------------------
System.NotSupportedException: Specified method is not supported.
at
NHibernate.Linq.Visitors.QueryModelVisitor.VisitAdditionalFromClause(AdditionalFromClause
fromClause, QueryModel queryModel, Int32 index)
at
Remotion.Data.Linq.Clauses.AdditionalFromClause.Accept(IQueryModelVisitor
visitor, QueryModel queryModel, Int32 index)
at
Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1
bodyClauses, QueryModel queryModel)
at
Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel
queryModel)
at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
at
NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel
queryModel, VisitorParameters parameters, Boolean root)
at
NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor
sessionFactory)
at
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String
queryIdentifier, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 filters,
ISessionFactoryImplementor factory)
at
NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String
expressionStr, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 enabledFilters,
ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
expressionStr, IQueryExpression queryExpression, String
collectionRole, Boolean shallow, IDictionary`2 enabledFilters,
ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String
expressionStr, IQueryExpression queryExpression, Boolean shallow,
IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at
NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
at
NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression
queryExpression, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression
queryExpression)
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)

Stefan Wenig

unread,
Sep 17, 2010, 10:50:05 AM9/17/10
to nhusers
OK, I can see it now.

The LINQ provider expects the expression to be:
<QuerySourceReferenceExpression> . <Member>

However, in the case of from brw in loan.Application.Borrowers it is:
<QuerySourceReferenceExpression> . <Member> . <Member>

So it's definately an unsupported feature, I can't think of a
workaround from the top of my head. The comment indicates that this
scenario was not considered (// What's this?)
I'd just file an issue.

Or try to remove this condition in
NHibernate.Linq.Visitors.QueryModelVisitor.VisitAdditionalFromClause
and build it:
if (member.Expression is QuerySourceReferenceExpression)
It might work, the HqlGeneratorExpressionTreeVisitor.Visit method at
least has code to handle MemberExpressions.

Cheers,
Stefan



On Sep 17, 4:24 pm, Aaron Fischer <pretzelfi...@gmail.com> wrote:
> Sorry about that,  I garbed the error message with out thinking.
> -----------------------------------
> System.NotSupportedException: Specified method is not supported.
>    at
> NHibernate.Linq.Visitors.QueryModelVisitor.VisitAdditionalFromClause(Additi­onalFromClause
> fromClause, QueryModel queryModel, Int32 index)
>    at
> Remotion.Data.Linq.Clauses.AdditionalFromClause.Accept(IQueryModelVisitor
> visitor, QueryModel queryModel, Int32 index)
>    at
> Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollect­ion`1
> bodyClauses, QueryModel queryModel)
>    at
> Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel
> queryModel)
>    at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
>    at
> NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel
> queryModel, VisitorParameters parameters, Boolean root)
>    at
> NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor
> sessionFactory)
>    at
> NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(S­tring
Reply all
Reply to author
Forward
0 new messages