2

I am using Entity Framework 5 and I have these classes:

public partial class Subject
{
    public int SubjectId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Topic> Topics { get; set; }
}

public partial class Topic
{
    public int TopicId { get; set; }
    public string Name { get; set; }
    public int SubjectId { get; set; }
    public virtual Subject Subject { get; set; }
    public virtual ICollection<SubTopic> SubTopics { get; set; }
}

public partial class SubTopic
{
    public int SubTopicId { get; set; }
    public string Name { get; set; }
    public int TopicId { get; set; }
    public virtual Topic Topic { get; set; }
}

Now I am trying to write a LINQ query to populate this class:

public class TopicSubTopicSelect
{
    public int TopicId { get; set; }
    public int SubTopicId { get; set; }
    public string TopicName { get; set; }
    public string SubTopicName { get; set; }
}

So far I have this:

        return _subjectsRepository
            .GetAll()
            .Where(s => s.SubjectId == subjectId)
            .Include(s => s.Topics)
            .Include(s => s.Topics.) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            .AsEnumerable()
            .Select(item => new TopicSubTopicSelect(item.TopicId,
                                                    item.SubTopicId,
                                                    item.Topic.Name,
                                                    item.Name))
            .ToList();

But it gives me an error on the line where I put <<<<<

What I wanted was to have .Include(s => s.Topics.SubTopics)

However intellisense does not give me this as an option. Any ideas what I am doing wrong and how I can modify the LINQ to get the data to fill the TopicSubTopicSelect class

  • 2
    When asking questions about linq, don't forget to include information about which provider you're using. Linq-to-Objects, entity framework, Linq-to-SQL etc. are all pretty different. – CodesInChaos Jul 27 '13 at 16:22
  • Sorry it's Entity Framework –  Jul 27 '13 at 16:29

2 Answers2

2
.Include(s => s.Topics.Select(t => t.SubTopics))

Use .Select() within the .Include() to get the desired join.

Making a simple project to test, I received the following:

screenshot

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • What is the difference between Select and SelectMany ? –  Jul 27 '13 at 16:25
  • Brad - Can you confirm what Rohit said. So should I use SelectMany? Also is one .Include line needed and how can I get the lower level fields to populate my view ? –  Jul 27 '13 at 16:30
  • @Melina: I believe `.Select` is what you want. http://s21.postimg.org/73gbwoa07/Untitled.png – Brad Christie Jul 27 '13 at 16:50
2

This will give you desired result -

.Include(s => s.Topics.SelectMany(t => t.SubTopics))

Use .Select if SubTopic is a property but if is a list, use .SelectMany.

For more clarification refer to Select Vs SelectMany.

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks. This works good. So can I confirm I only need the one .Include line? Also what would the .Select look like? I tried ".Select(item => new TopicSubTopicSelect(item.Topics." but then have a problem there as I could not get the TopicID? –  Jul 27 '13 at 16:28
  • I will open up another question regarding the above. Accepted your answer. Thanks –  Jul 27 '13 at 16:38