Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想在dotnetcore web api控制器中使用linq方法从entityframework的多个表中获取数据_C#_Linq_Asp.net Core_Model View Controller_Webapi - Fatal编程技术网

C# 我想在dotnetcore web api控制器中使用linq方法从entityframework的多个表中获取数据

C# 我想在dotnetcore web api控制器中使用linq方法从entityframework的多个表中获取数据,c#,linq,asp.net-core,model-view-controller,webapi,C#,Linq,Asp.net Core,Model View Controller,Webapi,//我需要这个格式的列表。我可以使用哪种linq查询方法从数据库中获得这样的结果 "id": 1, "name": "ID-1", "duration": 3600, "aircraft": "PT-6", "syllabus": "BASIC CONV", "missionTyp

//我需要这个格式的列表。我可以使用哪种linq查询方法从数据库中获得这样的结果

    "id": 1,
    "name": "ID-1",
    "duration": 3600,
    "aircraft": "PT-6",
    "syllabus": "BASIC CONV",
    "missionTypeId": 1,
    "phaseId": 1,
    "type": null,
    "phase": null
}
请尝试以下代码:

    {
        "id": 1,
        "name": "ID-1",
        "duration": 3600,
        "aircraft": "PT-6",
        "syllabus": "BASIC CONV",
        "missionTypeId": 1,
        "phaseId": 1,
        "type": {
            "missionTypeId": 1,
            "missionTypeName": "ID - 1 "
        },
        "phase": {
            "phaseId": 1,
            "phaseName": "ID"
        }
    }


您需要明确告诉EF Core要加载哪些相关实体

试试这个:

等待上下文。任务
.Include(m=>m.Type)
.包括(m=>m.Phase)
.ToListAsync();

这不是问题。请告诉我们你想做什么,你不能做什么,特别是你需要什么帮助。
    {
        "id": 1,
        "name": "ID-1",
        "duration": 3600,
        "aircraft": "PT-6",
        "syllabus": "BASIC CONV",
        "missionTypeId": 1,
        "phaseId": 1,
        "type": {
            "missionTypeId": 1,
            "missionTypeName": "ID - 1 "
        },
        "phase": {
            "phaseId": 1,
            "phaseName": "ID"
        }
    }

var mission = (from m in _context.Missions
                join t in _context.MissionTypes on m.MissionTypeId equals t.MissionTypeId
                join p in _context.Phases on m.PhaseId equals p.PhaseId
                select new Mission
                {
                    Id = m.Id,
                    Name = m.Name,
                    Duration = m.Duration,
                    Aircraft = m.Aircraft,
                    Syllabus = m.Syllabus,
                    MissionTypeId = m.MissionTypeId,
                    PhaseId = m.PhaseId,
                    Type = m.Type,
                    Phase = m.Phase
                }).FirstOrDefault();