I use LINQ a lot in general, especially LINQ-to-Objects, hence I'm rather fluent in LINQ.
I was considering to use LINQ-to-NHibernate as the query language for my NHibernate project. When I wrote some tests, I noticed that LINQ-to-NHibernate doesn't make the same query as ICriteria. Since I'd prefer to use LINQ, I'd like to ask if anyone knows of similar differences, or should I just not bother about performance in general (The high-performance operations would need some tweaking with NHibernate anyway as far as I get it). See the following example:
var query = (from inputItem in session.Linq<InputItem>()
where inputItem.Project == project select inputItem).First();
gives me the following SQL:
SELECT this_.ID as ID0_1_, this_.Name as Name0_1_, this_.Project_id as Project3_0_1_, project1_.ID as ID1_0_, project1_.Name as Name1_0_
FROM "InputItem" this_ left outer join "Project" project1_ on this_.Project_id=project1_.ID
WHERE this_.Project_id = @p0 limit 1;@p0 = 1, @p1 = 1
whereas
var criteria = session.CreateCriteria<InputItem>();
criteria.Add(Expression.Eq("Project", project));
criteria.SetMaxResults(1);
criteria.List();
gives
SELECT this_.ID as ID0_0_, this_.Name as Name0_0_, this_.Project_id as Project3_0_0_
FROM "InputItem" this_
WHERE this_.Project_id = @p0 limit 1;@p0 = 1, @p1 = 1
Clearly, the LEFT JOIN wouldn't be necessary.
Is something wrong with my LINQ query or is it just a limitation? And should I worry about it in the first place?
Icey
EDIT: I tried changing the LINQ statement to the following:
var query = (from inputItem in session.Linq<InputItem>()
where inputItem.Project.ID == project.ID
select inputItem).First();
the generated SQL is the same, though.
It looks to me as if NHibernate.Linq doesn't support this optimisation right now. I think you'll need to use a criteria query, or HQL, or wait until the fully integrate LINQ provider is released (slated for NHib v3 I think).
Cheers, John
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With