Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 返回包含子表集合的实体_Entity Framework - Fatal编程技术网

Entity framework 返回包含子表集合的实体

Entity framework 返回包含子表集合的实体,entity-framework,Entity Framework,以下是我所拥有的: public class StudentHealthInfoType { public int StudentHealthInfoId { get; set; } public bool? HasAllergies { get; set; } public List<HealthInfoMedicationType> HealthInfoMedicationType { get; set;} } public class HealthI

以下是我所拥有的:

public class StudentHealthInfoType
{

    public int StudentHealthInfoId { get; set; }
    public bool? HasAllergies { get; set; }
    public List<HealthInfoMedicationType> HealthInfoMedicationType { get; set;}
}

public class HealthInfoMedicationType
{
    public int HealthInfoMedicationId { get; set; }
    public string MedicationName { get; set; }
}

var result = (from u in context.StudentHealthInfos
                  from m in context.HealthInfoMedications
                  where u.RegistrationId == registrationId
                   && u.StudentHealthInfoId == m.StudentHealthInfoId
                   select new StudentHealthInfoType 
                   { StudentHealthInfoId =     u.StudentHealthInfoId,
                      HasAllergies = u.HasAllergies,                                                                    HealthInfoMedicationType = new HealthInfoMedicationType
                     { HealthInfoMedicationId = m.HealthInfoMedicationId,
                       MedicationName = m.MedicationName
                      }                                                                   
                    }).FirstOrDefault();
公共类学生健康信息类型
{
public int StudentHealthInfoId{get;set;}
公共布尔?HasAllergies{get;set;}
公共列表HealthInfoMedicationType{get;set;}
}
公共类HealthInfoMedicalationType
{
公共int HealthInfoMedicationId{get;set;}
公共字符串MedicationName{get;set;}
}
var result=(来自context.StudentHealthInfos中的u)
来自context.healthInformations中的m
其中u.RegistrationId==RegistrationId
&&u.StudentHealthInfoId==m.StudentHealthInfoId
选择新的StudentHealthInfo类型
{StudentHealthInfoId=u.StudentHealthInfoId,
HasAllergies=u.HasAllergies,HealthInfoMedicationType=new HealthInfoMedicationType
{HealthInfoMedicationId=m.HealthInfoMedicationId,
MedicationName=m.MedicationName
}                                                                   
}).FirstOrDefault();
我得到这个错误,它显示在healthinfomediationtype=newhealthinfomediationtype

无法隐式转换类型 “Dis.QueryManager.HealthFormTypes.HealthInfoMedicationType”到 'System.Collections.Generic.List'


HealthInfoMedicalationType需要是为一个StudentHealthInfoType记录返回的项目的集合。我需要如何设置我的对象,然后强制转换它们,以便此查询工作?

如果您想查询特定的注册id和您可以这样做的健康信息药物列表,请尝试此操作

var result = context.StudentHealthInfos
         .Include(x=>x.HealthInfoMedicationType).FirstOrDefault(f => f.RegistrationId == 1);

//结果将是StudentHealthInfoMedicationType和HealthInfoMedicationType列表

您能告诉我您要查询什么吗?预期结果var Result=context.StudentHealthInfos.Include(x=>x.HealthInfoMedicationType)。FirstOrDefault(f=>f.RegistrationId==1);什么类型是上下文。StudentHealthInfos?