Why does Nhibernate.Linq throws me exception "The method Compareto is not implemented"?
I've been struggling a long time with this problem and I can't get it to work properly. What I want to do, is to get entries with a date earlier than a specified entry. For you to understand, I'll to show you this code snippet:
var post = _session.Linq<PostModel>()
.Where(o => o.PostId == id)
.FirstOrDefault();
return _session.Linq<PostModel>()
.Where(o => DateTime.Compare(post.PostDate, o.PostDate) >= 0)
.ToList();
This doesn't work! It will throw me an exception saying:
The method Compareto is not implemented.
I've tried implementing "IComparer" to my PostModel, but that seems not be the problem here. Although, this will work if I first convert all the entries to a list, then compare them, like this:
return _session.Linq<PostModel>().ToList()
.Where(o => DateTime.Compare(post.PostDate, o.PostDate) >= 0)
.ToList()
But to my understanding, converting all entries to a list before any cases, will cause NHibernate selecting all entries. This might not be a problem if we're talking about a small amount of entries, but in the long term, this will waste 开发者_如何学Gosome time.
Does anyone have any ideas?
TIA
The issue is that the NHibernate linq provider needs to convert the call to DateTime.Compare
into SQL so it can be executed on the server. As the error states, this has not been implemented, although you may be able to change your query to:
_session.Linq<PostModel>()
.Where(o => post.PostDate >= o.PostDate)
.ToList();
which probably will be implemented.
Just a guess, but it may not be liking the static call in your where cause. Perhaps just try using the built in relational operators on datetime like this:
.Where(o => o.PostDate >= post.PostDate)
I think about trying to convert your version to SQL and I can't see how they would know to do it, even with all of the black magic NHibernate does.
Lee has it bang-on. The NHibernate LINQ provider has no way of converting the method call into SQL, which is why a NotImplementedException is being thrown. You can provide NHibernate with a way to translate this call into SQL via NHibernate's LINQ provider extension model:
http://fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html
精彩评论