tags:

views:

60

answers:

1

My model is like this:

Flavor
  public IList<Mention> Mentions

Mention
  public IList<Flavor> Flavors

Therefore, a many-to-many between Flavor and Mention.

I need to create a query that returns me the flavors mentioned and the amount of times it was mentioned.

Today, I can get the query terms ordered by the number of flavors mentioned but can not return the whole lot of times.

My hql query:

  select flavor
  from Flavor flavor
  left join flavor.Mention mentions
  group by flavor.Id, flavor.Name
  order by count(mentions) desc

Does anyone know how to do this?

+1  A: 

Try this:

var flavors = Session.CreateQuery(@"select f.Id, f.PropertyName, count(elements(f.Mentions)) 
                                   from Flavor f where exists elements(f.Mentions) group by f.Id, f.PropertyName").List();

I believe it does the trick...

tolism7
I try, but how can get count(elements(f.Mentions))? I can create one property in Flavor, per example, NumberOfMentions. How auto add count(elements(f.Mentions)) in NumberOfMentions ?Thank´s
Juliano Oliveira
The code only assumes that your mappings are as you have them (Flavor has a many-to-many property called: Mentions and Mention has a many-to-many property called: Flavors. That's it. You do not need to create any extra properties.
tolism7
I could not yet. I think I'll have to change my strategy.In addition to obtaining the top flavor, must be of "mentions" of specific type. (e.g: Mention.Type = TypeMention.Question).I must also order the amount of Flavor.Complicated?
Juliano Oliveira
Considering your new requirements try to break the query into 2 queries or more... I believe it gets very complicated if you try to squeeze everything in one query.
tolism7