0

I have these two classes:

public partial class Topic {
    public int TopicId { get; set; }
    public string Name { 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; 
}

Here's my current LINQ statement:

var subTopics = _subTopicsRepository
                .GetAll()
                .Where(s => s.TopicId == topicId)
                .ToList();

How can I make it so the subTopics variable also has the TopicId and the Topic.Name and then have this populate the view:

public partial class TopicSubTopic
{
    public int TopicId { get; set; }
    public int SubTopicId { get; set; }
    public string TopicName { get; set; }
    public string SubtopicName { get; set; }
}
3
  • you mean .Where(s => s.TopicId == topicId && s.TopicName == topicName)?
    – Ehsan
    Jul 27, 2013 at 13:17
  • @Ehsan - I don't have TopicName in the SubTopic :-(
    – user1943020
    Jul 27, 2013 at 13:23
  • see my answer below. if i understand your question correctly that is what you are looking for.
    – Ehsan
    Jul 27, 2013 at 13:35

3 Answers 3

2

This should do it

var subTopics = _subTopicsRepository
            .GetAll()
            .Where(s => s.TopicId == topicId && s.Topic.Name == "YourTopic")
            .Select new{TopicId  = s.Topic.TopicId,SubTopicId = s.TopicId,TopicName = s.Topic.Name,SubTopicName = S.Name};

then in your class just add a constructor

 public partial class TopicSubTopic {
    public TopicSubTopic(int topicID,int subTopicId,string topicName,string subTopicName)
    {
     TopicId = topicID;
     SubTopicid = subTopicID;
     TopicName = topicName;
     SubTopicName = subTopicName;
    }

    public int TopicId { get; set; }
    public int SubTopicId { get; set; }
    public string TopicName { get; set; }
    public string SubtopicName { get; set; } }

to get values and

 from item in subTopics.AsEnumerable()
          select new TopicSubTopic(item.Topicid,item.SubTopicid,item.TopicName,item.SubTopicName);
1

You need to join the two tables and then select columns from both tables into an annonymous object.

var topics = new System.Collections.Generic.List<Topic>();
var subTopics = new System.Collections.Generic.List<SubTopic>();
int topicId = 23984;
var details = from subtopic in subTopics
    where subtopic.TopicId == topicId
    join topic in topics on subtopic.TopicId equals topic.TopicId
    select new TopicSubTopic()
    {
        TopicId = topic.TopicId,
        SubTopicId = subtopic.SubTopicId,
        TopicName = topic.Name,
        SubtopicName = subtopic.Name
    };

http://msdn.microsoft.com/en-us/library/bb311040.aspx

1
  • The joining would be done by Entity Framework. Just looking for the LINQ syntax for now. Thanks.
    – user1943020
    Jul 27, 2013 at 13:29
1

You should be able to access the Topic through your virtual property. For example,

var subTopics = _subTopicsRepository
                .GetAll()
                .Where(s => s.TopicId == topicId && s.Topic.Name == "ExampleTopic")
                .ToList();

Edit: To populate the ViewModel (which contains a combination of your models) I would use something like AutoMapper. This will allow you to specify which properties from a source model maps to certain properties on your view model.

An example of the syntax for creating a mapping in this case would look like:

        Mapper.CreateMap<SubTopic, TopicSubTopic>()
            .ForMember(dest => dest.TopicId, opt => opt.MapFrom(src => src.Topic.TopicId))
            .ForMember(dest => dest.SubTopicId, opt => opt.MapFrom(src => src.SubTopicId))
            .ForMember(dest => dest.TopicName, opt => opt.MapFrom(src => src.Topic.Name))
            .ForMember(dest => dest.SubTopicName, opt => opt.MapFrom(src => src.Name));

This allows for the view to have everything it needs on one level, which is what I think you are going for.

3
  • Thanks Dan. I added a view to my question. I think your answer is good but how I can make this go into the view TopicSubTopic ?
    – user1943020
    Jul 27, 2013 at 13:23
  • Your answer uses mapper and I don't know about this yet. Is this something that's standard with .Net ? Could I do the same with a projection (think I have the right word) ?
    – user1943020
    Jul 27, 2013 at 13:31
  • Yes, there has been another answer provided with another way of doing it that might work better for you. I've used AutoMapper in the past, though, and have found it's good for code cleanliness. (It's a free open source .Net project) Jul 27, 2013 at 13:32

Your Answer

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