views:

161

answers:

2

Hi all,

I am trying to perform what I can only describe as the reverse version of IN() using the Nhibernate Criteria..

Rather than saying the value stored in the property is in the list/collection X

I want to say, this value X (an ID) is in the list which is a property of the object.

Any help appreciated, I can try to explain better if this makes no sense.

EDIT Apologies to people, as I understand this doesn't make enough sense...

I have an Event item in my system which can have a list of Audience (items) which it applies to. If the admin wants to delete an Audience item I want to check that the item is not referenced by any events. (The AudienceList is stored as a string list of IDs for Audiences).

What I was thinking was something along the lines of:

var results = SessionInstance.Session.CreateCriteria(typeof(EventItem.Items.EventItem)) .Add(Restrictions.In("AudienceList", myAudience.ID)) .List();

But I need the AudienceList and myAudience.ID to be the other way around don't I? I have the audience ID but need to check that it might be in a list of other ids.

Thanks again.

EDIT 2

The definition of the EventItem is such that one of it's properties is a DetailCollection of Audience IDs, they are saved to the DB as a string list of IDs.

A: 

Do you mean something like this?

class YourClass {
    List<int> theList;
}

IQueryable<YourClass> query = ...;
var result = from c in query where c.theList.Contains(value);
configurator
+2  A: 

EDIT: Scrapped my previous answer.

Well, it sounds like there is not a true relational model here so things will not be as pretty. Without foreign keys and the like, I'm not sure NHibernate will be much use in this situation (at least to generate a nice query).

If all of the Audience IDs for a single Event record are stored in a single field , you'll either have to use a SQL LIKE or do it in code. Fetch all the EventItems and iterate through them, checking their AudienceList collections for the ID you're looking for. If you go that route, it might be best to create a DTO to minimize the amount of data you move around. Something like

session.CreateQuery("select new AudienceListDTO(e.AudienceList) from EventItems e").List();

That's assuming you just need to inform the user that "this audience type is still in use" and not point out specific instances. Otherwise you'll have to add more data.

Stuart Childs
Hey, I'm not quite sure what the terminology is sorry. Everything is stored by the framework I am using on top of NHibernate (N2 CMS). The objects stored in the item table and the details of the object in the details table.In my case I have an Event which can have Audiences (Kids, Adults etc) I am wanting to check that an Audience isn't referenced by an Event when trying to delete the Audience item.
peteski22
Ok, I'm pretty sure my answer is quite wrong. I'll update it but first could you clarify the class definition of EventItem? Is AudienceList List<string> or List<Audience> or just string (comma separated perhaps?) or ...?
Stuart Childs
Hey, thanks for that answer! I'll give it a shot. Should there be a different relationship? I'd rather do it the right way going forward.ThanksPete
peteski22
It sounds like you have a many-to-many relationship between EventItems and Audiences. That is, EventItems can target many different audiences and Audiences can by targeted by many different events. Ideally then, what you'd have is 3 tables, 1 for EventItems, 1 for Audiences, and 1 "join" or "link" table to hook up EventItems to Audiences. http://sdesmedt.wordpress.com/2006/10/21/nhibernate-part-5-mapping-techniques-for-aggregation-many-to-one-and-many-to-many-mapping/ That link might be helpful for implementing many-to-many in NHibernate if you are not familiar with it.
Stuart Childs