6

I am having a problem with duplicate blog post coming back when i run the linq statement below.

The issue that a blog post can have the same tag more then once and that's causing the problem. I know when you use criteria you can do the followingcriteria.SetResultTransformer(new DistinctRootEntityResultTransformer());

How can I do the same thing with linq?

List<BlogPost> result = (from blogPost in _session.Linq<BlogPost>()
                         from tags in blogPost.Tags
                         where tags.Tag == tag && blogPost.IsPublished 
                            && blogPost.Slug != slugToExclude
                         orderby blogPost.DateCreated descending
                         select blogPost).Distinct()
                        .Skip(recordsToSkip).Take(pageSize).ToList();
Michael Maddox
  • 12,331
  • 5
  • 38
  • 40
  • Hm... The problem is that NH doesn't translate .Distinct() expression properly? Looks like a bug in translator. – Alex Kofman Mar 12 '10 at 07:41
  • It's not a bug because the DateCreated would be unique for each row? I suggest you use an equivalent of MAX(DateCreated) otherwise you might need to partition or refactor in some other way to perform a primary filter. – Phil C Feb 13 '11 at 10:02

1 Answers1

2

try

List<BlogPost> result = (from blogPost in _session.Linq<BlogPost>()
                         where blogPost.Tags.Any(t => t == tag) 
                            && blogPost.IsPublished 
                            && blogPost.Slug != slugToExclude
                         orderby blogPost.DateCreated descending
                         select blogPost).Distinct()
                        .Skip(recordsToSkip).Take(pageSize).ToList();

The old NHibernate LINQ provider is not maintained anyway. Try the new one built-in of NHibernate 3.0 (to use it you type session.Query() instead of session.Linq().

Meligy
  • 35,654
  • 11
  • 85
  • 109