Question

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

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top