如何将包含表中的字段包含在LINQ表达式中

本文关键字:包含 LINQ 表达式 字段 包含表 | 更新日期: 2023-09-27 18:25:20

我有这两个类:

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

如何将包含表中的字段包含在LINQ表达式中

这应该是

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

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

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

您应该能够通过虚拟属性访问Topic。例如,

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

这允许视图在一个层面上拥有所需的一切,这就是我认为你要追求的。