@Manish.
Let's say that Expression is the text that represents the function, and func
is the function you get when you compile that text (over simplification).
So, the difference is that when LINQ gets a text it can parse to extract the
where part, it works, and if it finds a compiled function it gets the entire
collection and brings every row to the function to check.

Hanselman write about it, although this is just the sense of it not the
details:
http://www.hanselman.com/blog/TheWeeklySourceCode52YouKeepUsingThatLINQIDunnaThinkItMeansWhatYouThinkItMeans.aspx

Now, one more catch is how to use it. Unfortunately you cannot use it with
the query syntax (the "from ... in ... where .. select .."). You have to use
the LINQ methods.

Changing your example to use Expression::

       private void Test( Expression<Func<Person, bool>> predicate )
       {
           var query = Linq<Person>("person").Where( predicate );

           var resultList = query.ToList();

           Assert.That(resultList,
           Has.All.AssignableTo(typeof(Person)));
       }


Side Note:
The NHIbernare LINQ you are using (Recognized by seeing Session.Linq<...>())
is pretty old and no longer maintained. I advise you to the the LINQ
provider in NHibernate itself. This is available in NHibernate 3.0 (which is
pretty much backwards compatible with NHibernate 2.1.2 GA). You can either
download the latest alpha or just the latest source code from SVN "trunk"
folder. (yes, both are usually high quality, due to thousands of unit tests
in there and also so many people using them in production, I'm one). Then
you'll replace Session.Linq<>() with Session.Query().
Note that all mentioned about expression trees and LINQ pretty much applies
to any LINQ provider, not just those of NHibernate.


Hope this helps.

Regards,



*Mohamed Meligy
*Readify | Senior Developer

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

 [image: Description: Description: Description: Description:
rss_16]<http://gurustop.net/>
 [image: Description: Description: Description: Description:
cid:[email protected]]
<http://www.linkedin.com/in/meligy>  [image:
Description: Description: Description: Description:
cid:[email protected]] <http://twitter.com/meligy>



On Wed, Sep 22, 2010 at 9:32 PM, manish.in.microsoft <
[email protected]> wrote:

> Thanks Oscar.
>
> I understood that NHibernate could not figure out that the predicate
> was a lamdbda expression.  If I change Func<Person, bool> to
> Expression<Func<Person, bool>>, how will the Linq query change?  I
> believe I will no longer be able to use from ... where predicate(e)
> select e?
>
> On Sep 22, 4:17 pm, Oskar Berggren <[email protected]> wrote:
> > 2010/9/22 manish.in.microsoft <[email protected]>:
> >
> >
> >
> > > I have a severe performance problem with Linq for NHibernate that I am
> > > able to reproduce with the Linq for NHibernate unit test cases as
> > > well.  Consider the following test case:
> >
> > >    [TestFixture]
> > >    public class EntityNameTests : BaseTest
> > >    {
> > >        [Test]
> > >        public void CanQueryOnEntityName()
> > >        {
> > >            var query = (from e in session.Linq<Person>("person")
> > >                         where e.Id == 1
> > >                         select e);
> >
> > >            var resultList = query.ToList();
> >
> > >            Assert.That(resultList,
> > > Has.All.AssignableTo(typeof(Person)));
> > >        }
> > >    }
> >
> > > When I run this test case, I see the following database query being
> > > fired:
> >
> > > SELECT this_.Id as Id7_0_, this_.Name as Name7_0_ FROM Person this_
> > > WHERE this_.Id = @p0;@p0 = 1
> >
> > > This is expected.  However, if I change the test case to:
> >
> > >    [TestFixture]
> > >    public class EntityNameTests : BaseTest
> > >    {
> > >        [Test]
> > >        public void CanQueryOnEntityName()
> > >        {
> > >            this.RunTest(e => e.Id == 1);
> > >        }
> >
> > >        private void Test(Func<Person, bool> predicate)
> > >        {
> > >            var query = (from e in session.Linq<Person>("person")
> > >                         where predicate(e)
> > >                         select e);
> >
> > In this case, you are saying:
> >
> > ... where SomeRandomFunctionCallThatMightDoWhateverItLikes(e) ...
> >
> > NHibernate has no choice but to get everything and pass each item to
> > the function. You need to use Expression<Func<T, bool>> instead.
> >
> > /Oskar
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

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

Reply via email to