I am wondering how do I order a group of results after a select with QueryOver. My query is the following:
CurrentSession.QueryOver<Book>()
.Where(b => b.Author.Name = "SimpleName")
.Select(Projections.Distinct(Projections.Property<Book>(b => b.Genre)))
.OrderBy<Genre>(g => g.Name) // this extension does not exist! How do I order for a Genre?
.List<Genre>()
How can I do?
Your query won't work to begin with. You first of all need to do a join, then you can do your order by and select projections.
Author author = null;
Genre genre = null;
CurrentSession.QueryOver<Book>()
.JoinAlias(b => b.Author, author)
.JoinAlias(b => b.Genre, genre)
.Where(() => author.Name == "SimpleName")
.OrderBy(() => genre.Name)
.Select(Projections.Distinct(Projections.Property<Book>(b => b.Genre)))
.List<Genre>();
Related
I need help converting this sql query into QueryOver Nhibernate criteria.
select distinct * from event e where e.name like '%req%'
or e.Id in (select r.eventId from requirement r where r.name like '%req%')
or e.Id in (select r.eventId from requirement r where r.id
in (select s.requirementId from solution s where s.name like '%sol%'))
var queryOver = session.QueryOver<Event>()
.Where(x => x.Name.IsInsensitiveLike("%"+searchTerms[1]+"%"))
.OrderBy(x => x.CreatedOn).Asc;
So far I have the main query but couldn't find enough reference material on how to add the subqueries. Haven't been successful using joinQueryOver.
Event has one-to-many rel with requirement and requirement has one-to-many rel with solution.
Requirement reqAlias = null;
Solution solAlias = null;
var subQuery = QueryOver.Of<Event>()
.JoinAlias(x => x.Requirements, () => reqAlias)
.Where(x => x.Name.IsInsensitiveLike(searchTerms[2]))
.JoinAlias(() => reqAlias.Solutions, () => solAlias)
.Where(x => x.Name.IsInsensitiveLike(searchTerms[3]))
.Select(Projections.Group<Event>(x => x.Id));
var events = session.QueryOver<Event>()
.Where(x => x.Name.IsInsensitiveLike(searchTerms[1]))
.WithSubquery.WhereProperty(x => x.Id).In(subQuery)
.List().ToList();
still not working.
When you use IsInsensitiveLike NHibernate appends the % after parsing, and uses lower to do a lower case comparison. In your code, you are appending the % yourself, which results in,
select distinct * from event e where e.name like %lower('%req%')%
which in turn, doesn't work.
Also, you have 3 subqueries, no a big one, so you need to restructure your code to account for that:
select r.eventId from requirement r where r.name like '%req%'
to
var firstQuery = QueryOver.Of<Requirement>()
.Where(r => r.Name.IsInsensitiveLike(searchTerms[2]))
.Select(r => r.EventId);
then,
select s.requirementId from solution s where s.name like '%sol%'
to
var solutionQuery = QueryOver.Of<Solution>()
.Where(s => s.Name.IsInsensitiveLike(searchTerms[3]));
then,
select r.eventId from requirement r where r.id
in (select s.requirementId from solution s where s.name like '%sol%')
to
var requirementQuery = QueryOver.Of<Requirement>()
.WithSubquery
.WhereProperty(r => r.Id).In(solutionQuery)
.Select(r => r.EventId);
Then you need to construct the main query using Restrictions.Or to include the 3 queries.
I'm writing part of turist blog so I have two entities: place and photo in relation 1 to many, so every place can have multiple photos, but single photo belongs to one place.
My aim is to create summary: last added 3 places and with every place I want to have one photo (for example photo with the lower id, it doesn't matter). It's important to have not only the id of the photo, but more info (columns) like photo label, path to photo,etc.
Using SQL in MS SQL 2008 it can be done by using rank functions:
select top 3 * from
(
SELECT p.id, p.label, p.VisitDate, ph.id AS photo_id, ph.Label, ph.Path,
RANK() OVER (PARTITION BY p.id ORDER BY ph.id) AS _rank
FROM place AS p INNER JOIN
photo AS ph ON p.id = ph.place_id
) ranked
where _rank = 1
order by VisitDate desc
Or it can be resolved without ranking:
SELECT TOP (3) a.id AS ida, z.id AS idz, z.etykieta, z.sciezka, a.data_odwiedzenia
FROM place AS a INNER JOIN
photo AS z ON a.id = z.id_atrakcji
and z.id in
(
SELECT min(z.id) AS idz
FROM photo z
where z.id_atrakcji in (select top 3 a.id from place a order by a.data_dodania desc)
group by z.id_atrakcji
)
ORDER BY a.data_odwiedzenia DESC
I started writing this query without ranking, but I've stucked.
summaryList =
session.QueryOver(() => placeAlias)
.JoinAlias(o => o.Photos, () => photoAlias)
.SelectList(list => list
.Select(() => placeAlias.Id).WithAlias(() => summaryAlias.PlaceId)
.Select(() => placeAlias.Etykieta).WithAlias(() => summaryAlias.PlaceName)
.Select(() => photoAlias.Etykieta).WithAlias(() => summaryAlias.PhotoLabel)
.Select(() => photoAlias.Id).WithAlias(() => summaryAlias.PhotoId)
)
.OrderByAlias(() => placeAlias.VisitDate).Desc
.WithSubquery.WhereProperty(x => x.Id).In(lastPlaces)
.TransformUsing(Transformers.AliasToBean<PlacesSummary>())
.List<PlacesSummary>();
Can you help me with this? How to write lastPlaces queryover or maybe there's another approach?
Regards,
Macko
var subquery = QueryOver.Of(() => photoAlias)
.Where(photo => photo.Place.Id == placeAlias.Id)
.Select(Projections.Max<Photo>(photo => photo.Id));
var summaryList = session.QueryOver(() => placeAlias)
.OrderBy(() => placeAlias.VisitDate).Desc
.JoinQueryOver(() => placeAlias.Photos, () => photoAlias)
.WithSubquery.WhereProperty(photo => photo.Id).Eq(subquery)
.SelectList(list => list
.Select(() => placeAlias.Id).WithAlias(() => summaryAlias.PlaceId)
.Select(() => placeAlias.Etykieta).WithAlias(() => summaryAlias.PlaceName)
.Select(() => photoAlias.Etykieta).WithAlias(() => summaryAlias.PhotoLabel)
.Select(() => photoAlias.Id).WithAlias(() => summaryAlias.PhotoId)
)
.TransformUsing(Transformers.AliasToBean<PlacesSummary>())
.List<PlacesSummary>();
cant test it right now
it doesn't work as it should but you it is very close:
summaryList =
session.QueryOver(() => placeAlias)
.OrderByAlias(() => placeAlias.VisitDate).Desc
.JoinAlias(o => o.Photos, () => photoAlias)
**.WithSubquery.WhereProperty(()=> photoAlias.Id).Eq(subquery)**
.SelectList(list => list
.Select(() => placeAlias.Id).WithAlias(() => summaryAlias.PlaceId)
.Select(() => placeAlias.Etykieta).WithAlias(() => summaryAlias.PlaceName)
.Select(() => photoAlias.Etykieta).WithAlias(() => summaryAlias.PhotoLabel)
.Select(() => photoAlias.Id).WithAlias(() => summaryAlias.PhotoId))
.TransformUsing(Transformers.AliasToBean<PlacesSummary>())
.Take(3)
.List<PlacesSummary>();
It differs in only one line .WithSubquery where orginally it refers to Place instead it should refer to Photo. Now it's working and it produces following SQL:
SELECT TOP (3) this_.id as y0_, this_.PlaceName as y1_, photoalias1_.PhotoName as y2_, photoalias1_.id as y3_
FROM [place] this_ inner join [photo] photoalias1_ on this_.id=photoalias1_.id_place
WHERE photoalias1_.id = (SELECT min(this_0_.id) as y0_ FROM [photo] this_0_ WHERE this_0_.id_place = this_.id)
ORDER BY this_.DateVisit desc
It's working great but I have another question:
How to rewrite in QueryOver such SQL:
SELECT min(id)
FROM photo
where id in (... it doesn't matter what's here ...)
group by place_id
So I want only list of photo ids (only column) and this query will be used as subquery in another queryover. Can it be done by QueryOver syntax?
I got a rather tough query to convert from SQL to LINQ-to-Entities.
Here's my SQL code:
select c_id
from db.c
inner join db.i on c_id = i_c
inner join db.l on c_id = l_c
group by c_id
having count(distinct i_attributeX) > count(distinct l_attributeY)
I seem to have problems with the distinct in linq. Any suggestions?
Cheers
How's this:
var result = db.c
.Where(c =>
c.i.Select(i => i.attributeX).Distinct().Count() >
c.l.Select(l => l.attributeY).Distinct().Count()
)
.Select(c => c.id);
or alternatively
var result = db.c
.Where(c =>
c.i.GroupBy(i => i.attributeX).Count() >
c.l.GroupBy(l => l.attributeY).Count()
)
.Select(c => c.id);
I have a HQL query:
select max(l.Num) from SomeTable l group by l.Type, l.Iteration
How can I translate/convert it to QueryOver?
Following one:
var grouped = session.QueryOver<SomeTable>()
.SelectList(l => l
.SelectGroup(x => x.Type)
.SelectGroup(x => x.Iteration)
.SelectMax(x => x.Num));
will generate SQL:
SELECT
MAX(l.Num),
l.Type,
l.Iteration
FROM
SomeTable l
GROUP BY
l.Type,
l.Iteration
which is not what I expect – I don’t want to have Type and Iteration in Select.
I'm using that query as subquery for select z from c where z IN (subquery).
try with this statement, I've used Aliases and UnderlyingCriteria
SomeTable someTb = null;
var grouped = session.QueryOver<SomeTable>(() => someTb)
.SelectList(l => l.SelectMax(() => someTb.lnum))
.UnderlyingCriteria.SetProjection(
Projections.Group(() => someTb.Type)
,Projections.Group(() => someTb.Iteration))
.List();
I hope it's helpful.
I want to convert a NHibernate CreateCriteria over to a NHLambdaExtensions criteria, but I'm getting errors that I don't know how to fix.
The NHibernate criteria looks like this:
var departments = DepartmentService
.CreateCriteria()
.CreateAlias( "Goals", "goal" )
.Add( Expression.Eq( "goal.Company.Id", companyId ) )
.Add( Expression.Eq( "goal.Program.Id", programId ) )
.List<Business.Department>();
The NHLambdaExtensions criteria that I'm trying to create looks like this:
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria()
.CreateAlias<Business.Goal>( g => g.Department, () => goalAlias )
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
The error I'm getting is "Could not resolve property Department of: Business.Department". The error obviously has to do with "g => g.Department", and there is nothing in the original NHibernate query that has something similar, but there are no overloads that don't take the expression.
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria(typeof(Business.Department)) // need to specify the first criteria as Business.Department
.CreateCriteria<Business.Department>(d => d.Goals, () => goalAlias)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
Look for "Create Criteria Association With Alias" in NHibernate Lambda Extensions (V1.0.0.0) - Documentation
EDIT:
You can actually write this more efficiently as:
// no alias necessary
var departments = DepartmentService
.CreateCriteria<Business.Department>()
.CreateCriteria<Business.Department>(d => d.Goals)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
I haven't used NHLambdaExpressions (but it looks pretty cool and I'll definitely check it out soon), so I'm just guessing here. Could you do something like this:
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria()
.CreateCriteria((Business.Department g) => g.Goals, () => goalAlias)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
I think this will root a new criteria at Goals and assign an alias via goalAlias.