0

我有这两个类:

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; 
}

这是我当前的 LINQ 语句:

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

我怎样才能使 subTopics 变量也具有 TopicId 和 Topic.Name ,然后让它填充视图:

public partial class TopicSubTopic
{
    public int TopicId { get; set; }
    public int SubTopicId { get; set; }
    public string TopicName { get; set; }
    public string SubtopicName { get; set; }
}
4

3 回答 3

2

这应该这样做

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};

然后在你的类中添加一个构造函数

 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; } }

获取值和

 from item in subTopics.AsEnumerable()
          select new TopicSubTopic(item.Topicid,item.SubTopicid,item.TopicName,item.SubTopicName);
于 2013-07-27T13:30:43.987 回答
1

您应该能够通过您的虚拟财产访问该主题。例如,

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

编辑:要填充 ViewModel (其中包含您的模型的组合),我会使用类似AutoMapper的东西。这将允许您指定源模型中的哪些属性映射到视图模型上的某些属性。

在这种情况下创建映射的语法示例如下所示:

        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));

这允许视图在一个级别上拥有它需要的一切,这就是我认为你想要的。

于 2013-07-27T13:21:46.933 回答
1

您需要连接这两个表,然后从两个表中选择列到一个匿名对象中。

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

于 2013-07-27T13:27:58.457 回答