Получение данных из нескольких таблиц в одном запросе SQL с использованием LINQ и Entity Framework - PullRequest
0 голосов
/ 27 апреля 2020

Привет, ребята. Я пытаюсь получить данные из нескольких таблиц в одном SQL запросе с использованием LINQ и Entity Framework. Мне нужно получить подробности из базы данных.

Я ссылался на этот Как фильтровать массив JSON в 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; }
    }

Ожидаемый результат:

{ "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}
                            ]
                },
            ]
  },
] }

1 Ответ

0 голосов
/ 28 апреля 2020

По вашим комментариям спецификации: используйте 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 
}
...