使用 LINQ 和实体框架在一个 SQL 查询中从多个表中提取数据

Posted

技术标签:

【中文标题】使用 LINQ 和实体框架在一个 SQL 查询中从多个表中提取数据【英文标题】:Pull data from multiple tables in one SQL query using LINQ and Entity Framework 【发布时间】:2020-08-10 18:18:05 【问题描述】:

大家好,我正在尝试。 我需要从数据库中获取详细信息。

我参考了这个How to filter JSON array in C# 我无法做到这一点。任何人的帮助将不胜感激。

public partial class Regions
    
        public Regions()
        
            Projects = new HashSet<Projects>();
            RegionUserRelation = new HashSet<RegionUserRelation>();
        

        public int RegionId  get; set; 
        [Required]
        public string RegionName  get; set; 
        [Required]
        public string RegionFullname  get; set; 
        [Required]
        public int RegionTypeId  get; set; 
        public string RegionMisc  get; set; 

        public RegionType RegionType  get; set; 
        public ICollection<Projects> Projects  get; set; 
        public ICollection<RegionUserRelation> RegionUserRelation  get; set; 
    

public partial class Projects
    
        public Projects()
        
            Sections = new HashSet<Sections>();
        

        public Guid ProjectId  get; set; 
        [Required]
        public string ProjectName  get; set; 
        [Required]
        public string ProjectCode  get; set; 
        public string ProjectDesc  get; set; 
        [Required]
        public int RegionId  get; set; 
        [Required]
        public int EpsgId  get; set; 

        public Epsg Epsg  get; set; 
        public Regions Region  get; set; 
        public ICollection<Sections> Sections  get; set; 
    


public partial class Sections
    
        public Sections()
        
            Flights = new HashSet<Flights>();
        

        public Guid SectionId  get; set; 
        [Required]
        public string SectionName  get; set; 
        public string SectionDesc  get; set; 
        [Required]
        public Guid ProjectId  get; set; 

        public Projects Project  get; set; 
        public ICollection<Flights> Flights  get; set; 
    

预期的输出是 Like

 "Regions" : 
  [  
        "region_id" : "2012",
        "region_name" : "region1",
        "region_fullname" : "21700",
        "region_type" : "MERCH",
            "Projects" : [ 
                    "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            "Projects" : [ 
                    "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            ]
  ,
   
        "region_id" : "2013",
        "region_name" : "region3",
        "region_fullname" : "21330",
        "region_type" : "MERCH",
            "Projects" : [ 
                    "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            "Projects" : [ 
                    "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            ]
  ,
   
        "region_id" : "2012",
        "region_name" : "region2",
        "region_fullname" : "24300",
        "region_type" : "MERCH",
            "Projects" : [ 
                    "project_id" : 2341,
                     "project_name " : "DXY",
                     "project_code " : "21700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            "Projects" : [ 
                    "project_id" : 1321,
                     "project_name " : "Proj_2",
                     "project_code " : "01700",
                     "sections" : [
                                 "section_id" : 4356,
                                "section_name" : sec_name,
                                "section_description" : sec_dis,
                                 "section_id" : 476,
                                "section_name" : sec_name2,
                                "section_description" : sec_dis2,
                                 "section_id" : 908,
                                "section_name" : sec_name3,
                                "section_description" : sec_dis3
                            ]
                ,
            ]
  ,
] 

【问题讨论】:

由于没有控制器或数据的代码,您将获得很多反对意见稍后查看此官方文档教程docs.microsoft.com/en-us/aspnet/core/tutorials/… @Pribina 是否有可能实现这一点。我检查了许多对我不起作用的教程。请帮助我摆脱困境。 应该是可以的,只要创建DTO类——在VS中你可以使用粘贴特殊Json作为类,然后crete查询映射所有属性 @Pribina 我能得到任何帮助吗.. 我对这个概念完全陌生 哪些步骤对您从数据库中检索数据或映射数据不起作用?您可以添加代码如何从数据库中检索数据吗?以及您在控制器中尝试了什么? 【参考方案1】:

根据您的 cmets 规范:使用 Include,然后使用 ThenInclude。您的模型中有导航属性,因此您可以利用它来发挥自己的优势 - 参考链接 - https://docs.microsoft.com/en-us/ef/core/querying/related-data

using (var context = new YourContex())

    var regions = context.Regions
        .Include(blog => blog.Projects)
            .ThenInclude(post => post.Sections)
        .ToList()
        .Select(region => new YourDTO()); //you need to specify this by your expectation or just omit it and keep original model 

【讨论】:

以上是关于使用 LINQ 和实体框架在一个 SQL 查询中从多个表中提取数据的主要内容,如果未能解决你的问题,请参考以下文章

在实体框架中从 SQL Server 告诉 MySQL

动态 SQL 到 LINQ 实体框架

实体框架/Linq to SQL:跳过和采取

使用 SQL 和 Linq 的多对多关系(实体框架/实体)

实体框架/LINQ/SQL 与实体框架/LINQ/MYSQL

如果不支持包含,您如何在 LINQ to Entities(实体框架)中执行 SQL 样式的“IN”语句?