Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top