Hello,

Just getting started with NH and have run into a hitch with a query
I'm trying to create via the ICriteria approach. I'm certain that this
is a fairly simple thing and the reference docs seem to get me close,
but here's an example-

Given two tables, Products and Categories, where each row in Products
has a "Category_Id" foreign key and a mapping where the Product class
has a Category instance member... I want a query that returns the
distinct list of the categories for an arbitrary list of products. In
SQL:

SELECT DISTINCT c.category_id, c.categoryname
FROM categories c
INNER JOIN products p ON p.category_id = c.category_id
WHERE p.unitprice < 40;

In code, I've got an ICollection<Product> that gets passed in as an
argument and want to return an ICollection<Category> accordingly. I'd
like to first do it assuming that the collection of products is
already filtered down to those that are $40 and under... but also want
to know how to add that criteria into the same query (as in the SQL).
>From the docs, it looks like I want to start with

// assume an ICollection<Product> called "prodColl"
var list = session.CreateCriteria(typeof(Category)).CreateCriteria
(typeof(Product))... /* to join? */
  /* And if I were including the $40 criteria, then I'd follow the
line above with:
      .Add(Expression.Lt("UnitPrice", 40))
  */
  /* - but what do I do with prodColl here to filter the resulting
category list? */
  .List<Category>();

I want to ensure that I'm only getting categories for the passed
collection of products. It seems one "brute force" approach would be
to iterate through the prodColl collection, grab the IDs, and then add
an Expression.In() criteria to the Product criteria, but that doesn't
feel right...

Then again, I'm not even 100% certain that chaining the two
CreateCriteria() calls like that is correct. Any pointers/guidance?

Thanks!


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to