Unique results from joined queries with NHibernate LINQ provider - nhibernate

I'm using NHibernate 2.1 with the LINQ provider and the results I get back from this query have multiple root nodes:
public IList<Country> GetAllCountries()
{
List<Country> results = (from country in _session.Linq<Country>()
from states in country.StateProvinces
orderby country.DisplayOrder, states.Description
select country)
.Distinct()
.ToList();
return results;
}
I know that using the Criteria API you can call DistinctRootEntityResultTransformer() to ensure that you get a unique root node, but I'm in the process of switching most of my queries over to the NHibernate LINQ provider, and I don't see an equavalient.
http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries.aspx

Using the NorthWind database, I wanted to get back the distinct regions from the territories... This syntax worked correctly.
(from t in Territories
from r in Regions
select new
{
r.RegionDescription
})
.Distinct().OrderBy(r => r.RegionDescription)
There is a post on a Microsoft forum here that may help.

Related

Multiple Fetches in linq to nhibernate

I was looking at this
Be careful not to eagerly fetch
multiple collection properties at the
same time. Although this statement
will work fine:
var employees =
session.Query()
.Fetch(e => e.Subordinates)
.Fetch(e => e.Orders).ToList();
I need to fetch 2 references so I would need to do something like that. Is there a better way of doing this.
I can't do .ThenFetchMany() as it goes to the into the child objects but the ones I am after on the same level.
Well, the query will still return the results you want, but as stated, it will return a cartesian product, i.e. the SQL query will return count(e.Subordinates) * count(e.Orders) results, which could add up pretty quickly, especially if you have more than just two collections.
NHibernate introduced Futures with the 2.1 release. Unfortunately there seems to be no way in the current NHibernate 3.0 release to make them work with NHibernate.Linq (session.Query<T>()).
Futures allow you to perform multiple queries in one roundtrip to the database (as long as the DB supports it, but most do). In that case you will only have count(e.Subordinates) + count(e.Orders) results, which is obviously the minimum.
Futures work with the criteria API, HQL and they are supposed to work with the new QueryOver API (I have not tested that, yet).
NHibernate.Linq does have Query().ToFuture() and Query().ToFutureValue(), but so far I only get Exceptions when I use them.
Edit:
I just checked again for the Linq API and it seems as if it is working if you do not use Fetch. The following will result in three SQL queries that are executed in one roundtrip. The total number of rows return will be 1 + count(Subordinates) + count(Orders).
int id = 1;
// get the Employee with the id defined above
var employee = repo.Session.Query<Employee>()
.Where(o => o.Id == id)
.ToFuture<Employee>();
// get the Subordinates (these are other employees?)
var subordinates = repo.Session.Query<Employee>()
.Where(o => o.HeadEmployee.Id == id)
.ToFuture<Employee>();
// get the Orders for the employee
var orders = repo.Session.Query<Order>()
.Where(o => o.Employee.Id == id)
.ToFuture<Order>();
// execute all three queries in one roundtrip
var list = employee.ToList();
// get the first (and only) Employee in the list, NHibernate will have populated the Subordinates and Orders
Employee empl = list.FirstOrDefault();
Thank you for having marked this as the answer anyway.

Query many-to-many without selecting all objects using Criteria API

I have a many-to-many relationship between Project and Site. I am trying to retrieve a list of Sites for a project using the Criteria API. I've got this working but the query also selects all of the columns for the associated Projects, which I don't want. I wrote what I thought was an equivalent query using HQL and it only selects the Site columns.
var target1 = session.CreateQuery("select s from Site s join s.Projects pr where pr.ProjectId = ?")
.SetInt32(0, projectId)
.List<Site>();
var target2 = session.CreateCriteria<Site>()
.CreateAlias("Projects", "pr")
.Add(Restrictions.Eq("pr.ProjectId", projectId))
.List<Site>();
How can I limit the Criteria API version (target2) to select only the Site columns? I tried using Projections but there's no method to project a type. I have to use Criteria API in this case.
I'm not sure if this is the best way, but I got it to work using SqlProjection:
Session.CreateCriteria<T>("foo")
.CreateAlias("foo.Bar", "bar")
.SetProjection(Projections.SqlProjection("{alias}.*", new string[] {}, new IType[] {}))
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(Foo)))
.List<Foo>();
This produces the following SQL:
SELECT this_.* FROM Foo this_ inner join Bar b1_ on this_.BarId=b1_.Id

Unable to figure out how to do Joins within IQueryable

Here is what I am trying:
IQueryable query = this.MyRepository.GetShippingCollection();
IList<SomeListType> myList = query.Where(x => x.Settings
.Where(y => y.SelectorID.Equals(5))
.Count() > 0)
.OrderBy(x => x.Order)
.ToList();
Produces this error:
could not resolve property: Settings.ID
If I do it this way it works, but causes over 3,000 queries on my SQL Server:
IList<SomeListType> myList = this.MyRepository.GetShippingCollection().ToList();
myList = myList.Where(x => x.Settings
.Where(y => y.SelectorID.Equals(5))
.Count() > 0)
.OrderBy(x => x.Order)
.ToList();
I know the solution resides within using a "Join".
I have been looking at examples for the last couple hours and can only find Join examples within the Mapping file. I am also finding examples for "ICriteria".
I don't want to have to create seporate entries for all my complex queries in the mapping file so the join within that file will not work.
Since I am using Fluent NHibernate, I am not using "ICriteria". I am using "IQueryable". How do you join multiple tables within "IQueryable"?
Any help with this would be greatly appreciated.
Thanks in advance.
If the second query is executing 3,000 queries, it is almost certainly lazy-loading the Settings collection. As you iterate over the list, you access this collection, and each time NHibernate goes back to the database to fetch it. Try setting the fetch mode for the Settings property to eager load in the mapping.
Beyond that, the LINQ provider could be an issue. What version of NHibernate are you using? The 2.x LINQ provider has some real limitations. It has been reimplemented in the 3.0 trunk, but you'll have to download and compile it from the source.
By the way, ICriteria vs IQueryable is not related to Fluent NHibernate. Criteria API and LINQ are two providers through which you can create queries. Fluent NHibernate is an alternative way to perform configuration.

NHibernate CreateCriteria query problem

I hope someone can help with this please.
I am trying to query an OLAP Fact table with NHibernate, but am struggling to get it to work. Its seems a simple requirement but I just cant see what the problem could be.
I have a central Fact table with several Dimension tables, one of the Dimensions has a secondary Dimension.
So ERD is. Fact >---1 Factor_Dim >---1 Target_Dim
My NHibernate query is.
facts = session.CreateCriteria(typeof(Fact), "facts")
.CreateAlias("facts.FactorDimension", "factDim", JoinType.InnerJoin)
.CreateAlias("factDim.TargetDimension", "targetDim",JoinType.InnerJoin)
.Add(Restrictions.Eq("targetDim.TargetID", targetId))
.List();
The error is "The multi-part identifier "targetdim2_.TargetID" could not be bound.". The generated SQL does not have the Factor_DIM or Target_DIM tables in the From clause.
Are there any alternative techniques to get this query to work? Id like to stick to this style as opposed to CreateSQLQuery() if possible.
Please help. Thanks.
Linq or QueryOver will be your cleanest solutions. If you are determined to stay with ICriteria you probably would want to wrap each of your entities with a class with common crud methods, it also makes your code access common, so code corrections are done in one place, not over hundres of files or classes.
Theres plenty of projects at http://nhforge.org/wikis/general/open-source-project-ecosystem.aspx which can help you out. I know NhGen ( http://sourceforge.net/projects/nhgen/ ) creates a CRUD class for each entity based on the NHibernate.Burrows GenericDao class with a few CRUD methods. It takes care of all the aliases and joins so queries become as simple as
IMessageDao messageDao = new MessageDao();
// Get All
IList<IMessage> messageList1 dao.FindAll();
// Find using QueryByExample
IList<IMessage> messageList2 = dao.FindByExample(messageDetails, orderBy)).ToList();
// Find using a simple entity query
IList<IMessage> messageList3 = messageDao.Find( new [] { Restrictions.Le(MessageHelper.Columns.Date, dateLastChecked) } );
// Find using a join and a query on said joined entities
IList<IMessage> messageList4 = messageDao.Find
( new []
{
Restrictions.Le(MessageHelper.Columns.Date, dateLastChecked),
Restrictions.Eq(MessageHelper.Columns.IsActive, true))
}, new[]
{
Restrictions.Eq(CategoryHelper.KeyColumns.Rsn, categoryRsn),
Restrictions.Eq(CategoryHelper.Columns.IsActive, true))
}, new []
{
Restrictions.Eq(ChannelHelper.KeyColumns.Rsn, channelRsn),
Restrictions.Eq(ChannelHelper.Columns.IsActive, true))
}
);
Theres plenty of overrides so you can specify your join type or it naturally assumes inner join.

LLBLGEN: Linq to LLBGEN don't work

I want to make custom select from the database table using Linq. We use LLBGEN as ORM solution.
I can't do LINQ query to Entities Collection Class unless I call GetMulti(null) method of it.
Is it possible to do LINQ query to LLBGEN without extracting all table first?
BatchCollection batches = new BatchCollection();
BatchEntity batch = batches.AsQueryable()
.Where(i => i.RegisterID == 3)
.FirstOrDefault(); // Exception: Sequence don't contains any elements
batches = new BatchCollection();
batches.GetMulti(null); // I don't want to extract the whole table.
BatchEntity batch = batches.AsQueryable()
.Where(i => i.RegisterID == 3)
.FirstOrDefault(); //Works fine
To query your database using LINQ (which is different from operating on an enumerable collection using the LINQ syntax) you have to use the LinqMetaData provider that comes with LLBLGen in the yourrootnamespace.Linq assembly. Once you add this assembly to your project you can use something like this to create your db query: (from the LLBL documentation)
LinqMetaData metaData = new LinqMetaData();
var q = from c in metaData.Customer
where c.Country=="USA"
select c;
In your example above you are using LINQ syntax to perform a where clause on a collection, but this does not have anything to do with LLBL or executing a query on the database. That is why it will not work with the empty collection, but does work on the filled collection.
Look more into LinqMetaData for the specifics of querying your db using LINQ to LLBLGen.