Linq to NHibernate Count with predicate

89 views
Skip to first unread message

Matt W

unread,
Nov 20, 2009, 11:04:45 AM11/20/09
to nhusers
Hello,

I'm not sure if this is the right spot to post this but here is the
issue I'm having with Linq to NHibernate.

I have a parent table and a child table and I'd like to do a query
like this:

var y = (from p in session.Linq<Parent>()
select new
{
p.ColumnA,
Count = p.Childern.Count(c=> c.ColumnC == "blah"),
}).ToList();

When this runs, I get this error:

NHibernate.QueryException was unhandled
Message="could not resolve property: Childern.ChildId of:
LToNHTest.Parent"
Source="NHibernate"
StackTrace:
at
NHibernate.Persister.Entity.AbstractPropertyMapping.GetColumns(String
propertyName)
at NHibernate.Persister.Entity.AbstractPropertyMapping.ToColumns
(String alias, String propertyName)
at
NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns
(String alias, String propertyName)
at NHibernate.Persister.Entity.AbstractEntityPersister.ToColumns
(String alias, String propertyName)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns
(String propertyName, ICriteria subcriteria)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumn
(ICriteria criteria, String propertyName)
at NHibernate.Criterion.CountProjection.ToSqlString(ICriteria
criteria, Int32 position, ICriteriaQuery criteriaQuery, IDictionary`2
enabledFilters)
at NHibernate.Criterion.ProjectionList.ToSqlString(ICriteria
criteria, Int32 loc, ICriteriaQuery criteriaQuery, IDictionary`2
enabledFilters)
at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetSelect
(IDictionary`2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor
(IOuterJoinLoadable persister, CriteriaQueryTranslator translator,
ISessionFactoryImplementor factory, ICriteria criteria, String
rootEntityName, IDictionary`2 enabledFilters)
at NHibernate.Loader.Criteria.CriteriaLoader..ctor
(IOuterJoinLoadable persister, ISessionFactoryImplementor factory,
CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2
enabledFilters)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria,
IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List()
at NHibernate.Linq.CriteriaResultReader`1.List() in C:\Work
\nhcontrib\trunk\src\NHibernate.Linq\src\NHibernate.Linq
\CriteriaResultReader.cs:line 21
at
NHibernate.Linq.CriteriaResultReader`1.<GetEnumerator>d__0.MoveNext()
in C:\Work\nhcontrib\trunk\src\NHibernate.Linq\src\NHibernate.Linq
\CriteriaResultReader.cs:line 26
at System.Collections.Generic.List`1..ctor(IEnumerable`1
collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at LToNHTest.Program.Main(String[] args) in C:\Work\spikes
\ntoltest\LToNHTest\LToNHTest\Program.cs:line 29
at System.AppDomain._nExecuteAssembly(Assembly assembly, String
[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


I believe my mapping is correct. If I change count to:

Count = p.Childern.Count(),

The query works just fine. I also tried Count = p.Childern.Where(c=>
c.ColumnC == "blah").Count(), and I get the same error.

I'm using the NHibernate 2.1.0, Fluent NHibernate 1.0 and the latest
version of Linq to NHibernate.

Any ideas?

Thanks!

-Matt

Steve Strong

unread,
Nov 22, 2009, 3:37:14 PM11/22/09
to nhu...@googlegroups.com
I'm going to put in a specific test for this, but I'm pretty sure that this construct will work just fine in the new Linq provider.  Of course, that's running against the trunk rather than 2.1, so I'm afraid I'm not helping you much!

Cheers,

Steve


--

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=.



Reply all
Reply to author
Forward
0 new messages