Kris

Kris

  • NA
  • 2
  • 0

IQueryable and Many-to-Many relationship

Mar 24 2009 7:22 AM

Hi folks,

I have a situation where i have several Categories, containing Articles. However, it is not just a simple Category-contains-Article relationship, but rather each Article has one Category set as its Main and several other Categorys set as Secondarys, ie:

public class Category
{
   ...
   public LazyList<Article> Articles { get; set
; }
   ...
}

public class Article
{
   ...
   public Category MainCategory { get; set
; }
   
public LazyList<Category> Categorys { get; set; }
   ...
}

I am populating in a repository with:

public
IQueryable<Category> GetCategorys()
{
   return from c in db.WOTS_Categories
 
      let articles = GetArticles(c.Id, true
)
      orderby
c.Name
      select new Category(c.Id, c.Name
)
      
{
         ...
         Articles = new LazyList<Article>(articles
),
         
...
      
};
}

and...

public IQueryable<Article> GetArticles()
{
   
return from a in
db.WOTS_Articles
   
let main = GetCategorysForArticle(a.Id, true).SingleOrDefault
()
   
let cats = GetCategorysForArticle(a.Id, false
)
   
orderby a.CreatedOn
descending
   
select new
Article
   
{
      
...
      
MainCategory = main
,
      
Categorys = new LazyList<Category>(cats
),
      ...
   
};
}

and...

public IQueryable<Category> GetCategorysForArticle(Guid articleId, bool isMain)
{
   
return from c in GetCategorys()
   
join ce in db.WOTS_CategoryEntries on c.Id equals ce.CategoryId
   
where ce.ArticleId == articleId && ce.IsMain == isMain
   
orderby c.Name
   
select c;
}

The problem i am having though, is it errors with "GetCategorysForArticle(System.Guid, Boolean)' has no supported translation to SQL"

Can anyone assist in diagnosing the problem, or suggesting a better way of achieving what i want?