0

I have the following entities

public class ArticleCategory
{
  public int Id {get; set;}
  public string Name {get; set;}
  public IList<Article> Articles {get; set;}
}

public class Article
{
  public int Id {get; set;}
  public string Name {get; set;}
  public ArticleCategory Category {get; set;}
}

public class JobArticles
{
  public int Id {get; set;}
  public Job Job {get; set;}
  public decimal Price {get; set;}
  public Article Article {get; set;}
}

As you can see Article knows nothing about to which JobArticle it has been assigned (it's not relevant)

So what I need to do is the following. Get every ArticleCategory for which there exist JobArticles for Job X.

The easiest way would be to Add the List of JobArticles to the Article Entity. But I'm not sure if it is the best way.

So I tried the opposite way (going from JobArticle to ArticleCategory). Something like that

IQueryOver<JobArticle, JobArticle> q = DataSession.Current.QueryOver<JobArticle>();

        Article ArticleAlias = null;
        ArticleCategory ArticleCategoryAlias = null;

        q.JoinAlias(x => x.Article, () => ArticleAlias);
        q.JoinAlias(x => ArticleAlias.Category, () => ArticleCategoryAlias);
        q.Where(x => x.Job.Id == jobId);
        q.SelectList(list => list
            .Select(x => ArticleCategoryAlias))

Which leads to a NULL-Reference Exception because .Select(x => ArticleCategoryAlias)

I'm not really sure how to do it, hope you can help

Arikael
  • 1,989
  • 1
  • 23
  • 60

1 Answers1

0
Article ArticleAlias = null;
ArticleCategory ArticleCategoryAlias = null;

var categories = DataSession.Current.QueryOver<ArticleCategory>()
    .WithSubquery.WhereProperty(x => x.Id).In(QueryOver.Of<JobArticle>()
        .JoinAlias(x => x.Article, () => ArticleAlias);
        .JoinAlias(x => ArticleAlias.Category, () => ArticleCategoryAlias);
        .Where(x => x.Job.Id == jobId);
        .Select(() => ArticleCategoryAlias.Id))
    .List();
Firo
  • 30,626
  • 4
  • 55
  • 94
  • sorry, took a long time, I just implemented and tested your solution. Didn't need it before (and couldn't test it) I'm not sure if it's a nhibernate version thing, but I had to change `.WithSubquery.Where` to `.WithSubquery.WhereProperty` – Arikael Mar 06 '13 at 13:27
  • As far i recall i wrote it from the top of my head. fixed. – Firo Mar 07 '13 at 18:39