Fabio,
Is session.Query<T> new?
Using what you have with session.Linq<T>:
var query = from v in _session.Linq<Vehicle>()
where v.Carrier ==
_session.Load<Carrier>(carrierId)
select v;
Generates the same sql as what I have in the my original Linq example:
NHibernate: SELECT this_.Id as Id1_1_, this_.VehicleType as
VehicleT2_1_1_, this_.BusinessRef as Business3_1_1_, this_.CarrierId
as CarrierId1_1_, carrier1_.Id as Id2_0_, carrier1_.Name as Name2_0_,
carrier1_.BusinessRef as Business3_2_0_, carrier1_.Enabled as
Enabled2_0_ FROM Vehicles this_ left outer join Carriers carrier1_ on
this_.CarrierId=carrier1_.Id WHERE this_.CarrierId = @p0;@p0 = 1
Thanks
Ben
On Jun 23, 12:38 pm, Fabio Maulo <[email protected]> wrote:
> from v in session.Query<Vehicle>() where v.Carrier ==
> session.Load<Carrier>(1) select v
>
>
>
> On Wed, Jun 23, 2010 at 8:16 AM, ben <[email protected]> wrote:
> > I've got a one to many association between a Carrier and Vehicle. In
> > my database my Vehicle table has a foreign key CarrierId.
>
> > The most efficient way to query vehicles by carrier would be:
>
> > select ... from Vehicles where Vehicles.CarrierId == 1
>
> > However, if I use QBC:
>
> > var fromDb = session.CreateCriteria<Vehicle>()
> > .CreateCriteria("Carrier",
> > global::NHibernate.SqlCommand.JoinType.InnerJoin)
> > .Add(Expression.Eq("Id", 1))
> > .AddOrder(new Order("BusinessRef", true))
> > .List<Vehicle>();
>
> > The result is:
>
> > NHibernate: SELECT this_.Id as Id1_1_, this_.VehicleType as
> > VehicleT2_1_1_, this_.BusinessRef as Business3_1_1_, this_.CarrierId
> > as CarrierId1_1_, carrier1_.Id as Id2_0_, carrier1_.Name as Name2_0_,
> > carrier1_.BusinessRef as Business3_2_0_ FROM Vehicles this_ inner join
> > Carriers carrier1_ on this_.CarrierId=carrier1_.Id WHERE carrier1_.Id
> > = @p0 ORDER BY carrier1_.BusinessRef asc;@p0 = 1
>
> > If I use NHibernate.Linq:
>
> > var fromDb2 = session.Linq<Vehicle>()
> > .Where(x => x.Carrier.Id == 1)
> > .OrderBy(x => x.BusinessRef)
> > .ToList();
>
> > The result is similar but always seems to do an outer join (how can I
> > force it to be left inner?):
>
> > NHibernate: SELECT this_.Id as Id1_1_, this_.VehicleType as
> > VehicleT2_1_1_, this_.BusinessRef as Business3_1_1_, this_.CarrierId
> > as CarrierId1_1_, carrier1_.Id as Id2_0_, carrier1_.Name as Name2_0_,
> > carrier1_.BusinessRef as Business3_2_0_ FROM Vehicles this_ left outer
> > join Carriers carrier1_ on this_.CarrierId=carrier1_.Id WHERE
> > carrier1_.Id = @p0 ORDER BY this_.BusinessRef asc;@p0 = 1
>
> > Finally if I use HQL (as much as I dislike using "magic" strings), the
> > result is exactly what I would expect and seems the most efficient:
>
> > var fromDb3 = session.CreateQuery("from Vehicle where
> > Carrier.Id = :id")
> > .SetParameter("id", 1)
> > .List<Vehicle>();
>
> > NHibernate: select vehicle0_.Id as Id1_, vehicle0_.VehicleType as
> > VehicleT2_1_, vehicle0_.BusinessRef as Business3_1_,
> > vehicle0_.CarrierId as CarrierId1_ from Vehicles vehicle0_ where
> > vehicle0_.carrier...@p0;@p0 = 1
>
> > So why do I get this difference and more importantly, how can I use
> > QBC or Linq to generate the same SQL as HQL does?
>
> > Thanks,
> > Ben
>
> > --
> > 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.
>
> --
> Fabio Maulo
--
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.