C# 使用LINQ和实体框架在一个SQL查询中从多个表中提取数据
大家好,我正在尝试使用LINQ和实体框架在一个SQL查询中从多个表中提取数据。 我需要从数据库中获取详细信息 这件事我曾做过裁判 我无法做到这一点。任何人的帮助都将不胜感激C# 使用LINQ和实体框架在一个SQL查询中从多个表中提取数据,c#,asp.net,asp.net-web-api,entity-framework-core,asp.net-core-2.0,C#,Asp.net,Asp.net Web Api,Entity Framework Core,Asp.net Core 2.0,大家好,我正在尝试使用LINQ和实体框架在一个SQL查询中从多个表中提取数据。 我需要从数据库中获取详细信息 这件事我曾做过裁判 我无法做到这一点。任何人的帮助都将不胜感激 public partial class Regions { public Regions() { Projects = new HashSet<Projects>(); RegionUserRelation = new Ha
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; }
}
根据您的注释说明:使用
包含
,然后使用然后包含
。您的模型中有导航属性,因此您可以在advantage-reference链接中使用它-
由于没有控制器代码或数据,您将获得大量的否决票,请稍后查看官方文档教程@Pribina是否有可能实现这一点。我查阅了很多教程,但都不适用于我。请帮助我解决这个问题。这应该是可能的,只需创建DTO类-在VS中,您可以使用粘贴特殊Json作为类,然后使用克里特查询来映射所有properties@Pribina我能得到什么帮助吗。。我是一个全新的概念。从数据库检索数据或映射数据时,哪些步骤不起作用?您可以添加代码来说明如何从数据库检索数据吗?你在控制器上试过什么?
{ "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}
]
},
]
},
] }
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
}