Positonic
Positonic

Reputation: 9411

Linq to nHibernate - exclude elements without child elements

var locations = (from location in session.Query<Location>()                                     
             where 
                (location.MB_ID == 0 || location.MB_ID == null) &&
                (location.hide != "Y" || location.hide == null) &&
                (location.locationNameRaw != "" && location.locationNameRaw != null) &&
                ((location.isIPCapableText != "" && location.isIPCapableText != null) || (
                    (location.ISDNNumber1 != null && location.ISDNNumber1 != "") ||
                    (location.ISDNNumber2 != null && location.ISDNNumber2 != "") ||
                    (location.ISDNNumber3 != null && location.ISDNNumber3 != "") ||
                    (location.ISDNNumber4 != null && location.ISDNNumber4 != "") ||
                    (location.ISDNNumber5 != null && location.ISDNNumber5 != "") ||
                    (location.ISDNNumber6 != null && location.ISDNNumber6 != "")                                        
                ))
                && (location.privateRoom == "N" || location.privateRoom == "" || location.privateRoom != null)
                && (
                        from lll in session.Query<LocationLonLat>()                                             
                        where
                            location.locationID == lll.locationId
                        select lll.locationId
                    ).Any()
                && (location.LastUpdatedTime > lastUpdateTime)
                && location.LocationTimes.Count() > 0
               /*&& (
                        from lt in session.Query<LocationTimes>()
                        where
                            location.locationID == lt.LID
                        select lt.LID
                    ).Any()*/
                select location
                  )
                  .ToList();

There is a relationship between Location (1) and LocationTimes (many), and I only want to return a dataset of locations that have at least one LocationTime record.

I tried a couple of things...

When I add the line:

&& location.LocationTimes.Count() > 0

or if I add the line:

  && (                      
     from lt in session.Query<LocationTimes>()
     where
    location.locationID == lt.LID
        select lt.LID
     ).Any()

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

I suspect that this may because of the size of the dataset or something...

Is there a better way of doing this? Like with a 'left outer join' or something?

Upvotes: 0

Views: 204

Answers (1)

kalki
kalki

Reputation: 526

I think a simple join should do it.

from locationTime in Query<LocationTime>()
join location in Query<Location>() on locationTime.Location.LocationId equals location.LocationId
join locationLat in Query<LocationLat>() on location.LocationLat.LocationLatId equals locationLat.LocationLatId
where ...
select location;

Upvotes: 1

Related Questions