开发者

Why is NHibernate Linq duplicating results?

I have a simple Nhibernate Linq query that is returning more results than expected:

  var result = (from foo in session.Linq<Foo>()
                      where foo.High.ID == High.ID
                      select foo).ToArray();

Foo looks like this:

public class Foo : DomainLayerSuperType
{
  // ...other members omitted for clarity
  protected IList<Bar> associatedBars;

}

开发者_JAVA百科My problem is I get a duplicated Foo for every Bar in the 'associatedBars' collection. So if there are 20 Bars in the collection for an instance of Foo, I get 20 Foo instances, each with 20 Bars.

Mapping for Foo: (FluentNhibernate)

//other mappings omitted
HasMany<Bar>(x => x.AssociatedBars)
                .Access.CamelCaseField()
                .AsBag()
                .Table("dbo.Bar")
                .KeyColumn("FooID")
                .Cascade.AllDeleteOrphan()
                .Inverse()
                .Fetch.Join(); //eager load

When I execute this equivalent Hql query, the problem doesn't occur:

var query = new StringBuilder();
query.AppendFormat("select foo from Foo foo where foo.High.ID = {0}", High.ID);
var result = session.CreateQuery(query.ToString()).List<Foo>().ToArray();

Also, when I change the mapping for Foo, to use Lazy loading for AssociatedBars, the problem doesn't occur.

Any ideas? Also, where is best forum for Nh Linq? I couldn't find one so posted here!


The problem is that current linq provider is based on criteria queries. When you are setting Fetch.Join property in mapping then if you check sql query that is generated you will see join there. You are getting repeated results becouse of this join.

In HQL NHibernate uses different way for generating sql query and sql will be wery similar to your HQL, thats why there is no join and no repeated columns.

For liq query you can try use Distinct extention for getting only unique results from a query.

There is a project currently in development to create a linq provider based on HQL parser

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜