4

When I execute the following query, I get an exception telling me that 'feedItemQuery' contains multiple items (so SingleOrDefault doesn't work).

This is expected behaviour when using the Criteria api WITHOUT the DistinctRootEntity transformer, but when using linq, I expect to get a single root entity (FeedItem, with the property Ads (of ICollection) containing all Ads).

Is there a way to tell NHibernate.Linq to use the DistinctRootEntity transformer?

My query:

var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads")
                   where ad.Id == Id
                   select ad;

var feedItem = feedItemQuery.SingleOrDefault(); // This fails !?

The mapping:

<class name="FeedItem" table="FeedItems" proxy="IFeedItem">
    <id name="Id" type="Guid">
        <generator class="guid.comb"></generator>
    </id>
 ...
    <set name="Ads" table="Ads">
        <key column="FeedItemId" />
        <one-to-many class="Ad" />
    </set>
</class>

Thanks in advance

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
Remco Ros
  • 1,467
  • 15
  • 31

2 Answers2

6

You can use the RegisterCustomAction method to set the result transformer:

var linqsession = session.Linq<FeedItem>();
linqsession.QueryOptions.RegisterCustomAction(c => c.SetResultTransformer(new DistinctRootEntityResultTransformer()));
var feedItemQuery = from ad in linqsession.Expand("Ads")
                    where ad.Id == Id
                    select ad
Simon
  • 5,373
  • 1
  • 34
  • 46
  • 2
    since the old Session.Linq does not exist any more and we now have the session.Query<>, is there a way to still specify this, i dont see the QueryOptions or RegisterCustomAction Property/method anymore – Dinesh Manne Feb 20 '13 at 17:17
0

var feedItemQuery = from ad in session.Linq().Expand("Ads")
where ad.Id == Id
select ad**.FirstOrDefault();**

  • kinda hacky (as we expect to get a Single result back), but I think this will do. I'll try it and update my question. – Remco Ros May 19 '09 at 15:38