0

I am trying to create a list of custom objects in LINQ, and I am not sure how to do it. Here is my classes...

public class MenuModel
{
    public IList<MenuCategoriesWithArticles> Menu { get; set; }
}

public class MenuCategoriesWithArticles
{
    public Category Category { get; set; }
    public IList<Article> Articles { get; set; }
}

and I would like to create MenuModel from the following functions that return Category and IList in order.

businessCategory.GetAllCategories();
businessArticle.GetArticlesByCategory(int categoryId);

I have something like below but I am not sure...

businessCategory.GetAllCategories().Select(x=> new .....)

any help would be great. I dont want to loop to get each categories' articles.

1
  • 1
    "I dont want to loop to get each categories' articles." I don't see how else you would do it given that GetArticlesByCategory() requires a categoryId. Commented Nov 21, 2016 at 19:25

2 Answers 2

2

Maybe something like this could help you.

businessCategory.GetAllCategories().Select(x=> new MenuCategoriesWithArticles{
   Category = x,
   Articles = businessArticle.GetArticlesByCategory(x.categoryId).ToList();
});

The only thing is, if GetArticlesByCategory does a database search this code won't be optimal; if that is the case, you should query all the articles separately depending on the categories you select first.

Sign up to request clarification or add additional context in comments.

Comments

1

I assume you are using Entity Framework. The best approach for you will be to create a relationship between articles and category in your sql server, change your classes that represent table to have references to each other, and let you context know about it.

public class Category
{
    public int CategoryId { get; set; }

    public IList<Article> Articles { get; set; }
}

public class Article
{
    public int ArticleId { get; set; }
    public Category Category { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Article>().HasOne(article => article.Category)
            .WithMany(category=> category.Articles);
    }

When you have this changes, you can get your categories this way.

await dbContext.Category.Include("Articles").ToListAsync()

After executing, your Category object will have list of Articles. You can find more info here http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

5 Comments

I cannot implement OnModelCreating, it says 'no suitable method found to overide'
That is weird. What version of Entity Framework are you using? I use EF Core and
I use EF core and my context class is defined like this: public partial class H2OContext : DbContext. If I navigate to DbContext definition, it has virtual method OnModelCreating.
I am using EF6.
Make sure that your context derives from DbContext and that you are implementing virtual method OnModelCreating in your context. There should be no problem. DbContext must have this method. You can find more info on this class here msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.