Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# 在复杂类型中使用Automapper_C#_Automapper - Fatal编程技术网

C# 在复杂类型中使用Automapper

C# 在复杂类型中使用Automapper,c#,automapper,C#,Automapper,我有一个像这样的EF后端属性 public partial class PatientSurveyQuestion { public PatientSurveyQuestion() { this.PatientSurveyQuestionAnswers = new HashSet<PatientSurveyQuestionAnswer>(); this.PatientSurveyReplies

我有一个像这样的EF后端属性

 public partial class PatientSurveyQuestion
    {
        public PatientSurveyQuestion()
        {
            this.PatientSurveyQuestionAnswers = new HashSet<PatientSurveyQuestionAnswer>();
            this.PatientSurveyReplies = new HashSet<PatientSurveyReply>();
        }

        public int QuestionID { get; set; }
        public string QuestionText { get; set; }
        public Nullable<int> PointID { get; set; }
        public Nullable<int> QuestionType { get; set; }

        public virtual PatientSurveyPoint PatientSurveyPoint { get; set; }
        public virtual ICollection<PatientSurveyQuestionAnswer> PatientSurveyQuestionAnswers { get; set; }
        public virtual ICollection<PatientSurveyReply> PatientSurveyReplies { get; set; }
    }

  public partial class PatientSurveyQuestionAnswer
    {
        public int QuestionID { get; set; }
        public int AnswerID { get; set; }
        public Nullable<System.DateTime> DateCreayed { get; set; }

        public virtual PatientSurveyAnswer PatientSurveyAnswer { get; set; }
        public virtual PatientSurveyQuestion PatientSurveyQuestion { get; set; }
    }
公共部分类PatientServeyQuestion
{
公共卫生服务问题
{
this.patientserveyquestionanswers=new HashSet();
this.patientserveyreplies=new HashSet();
}
public int QuestionID{get;set;}
公共字符串QuestionText{get;set;}
公共可为空的PointID{get;set;}
公共可为空的问题类型{get;set;}
公共虚拟PatientCurveyPoint PatientCurveyPoint{get;set;}
公共虚拟ICollection PatientServeyQuestionAnswers{get;set;}
公共虚拟ICollection PatientServeyReplies{get;set;}
}
公共部分课程耐心观察问题解答
{
public int QuestionID{get;set;}
public int AnswerID{get;set;}
公共可为空的DateCreayed{get;set;}
公共虚拟PatientServeyanSwer PatientServeyanSwer{get;set;}
公共虚拟患者服务问题患者服务问题{get;set;}
}
还有一个加入班

  public class PatientQS
    {

        public PatientSurveyQuestion Question { get; set; }
        public List<PatientSurveyAnswer> Answers { get; set; }
    }
公共类PatientQS
{
公共病人服务问题{get;set;}
公共列表答案{get;set;}
}
我也有相同的类,没有WFC的引用和虚函数

[DataContract]
public class Question
{
    [DataMember]
    public int QuestionID { get; set; }
    [DataMember]
    public string QuestionText { get; set; }
    [DataMember]
    public Nullable<int> PointID { get; set; }
    [DataMember]
    public Nullable<int> QuestionType { get; set; }
}

   [DataContract]
    public class Answers
    {
        [DataMember]
        public int AnswerID { get; set; }
        [DataMember]
        public string Answer { get; set; }
    }

   [DataContract]
    public class QuestionsAnswers
    {
        [DataMember]
        public Question Question { get; set; }
        [DataMember]
        public List<Answers> Answers{ get; set; }
    }
[DataContract]
公开课问题
{
[数据成员]
public int QuestionID{get;set;}
[数据成员]
公共字符串QuestionText{get;set;}
[数据成员]
公共可为空的PointID{get;set;}
[数据成员]
公共可为空的问题类型{get;set;}
}
[数据合同]
公开课答案
{
[数据成员]
public int AnswerID{get;set;}
[数据成员]
公共字符串答案{get;set;}
}
[数据合同]
公开课问答
{
[数据成员]
公共问题{get;set;}
[数据成员]
公共列表答案{get;set;}
}
然后我运行了一个查询来获取数据并使用AutoMaper

   var quesans = (from c in db.PatientSurveyQuestions
                       from v in db.PatientSurveyQuestionAnswers
                       where c.QuestionID == v.QuestionID && c.PointID == pointID
                       select new {c, ans=c.PatientSurveyQuestionAnswers.Select(n=>n.PatientSurveyAnswer)}).ToList().Select(c => new PatientQS { Question = c.c, Answers = c.ans.ToList() }).ToList();
                        Mapper.CreateMap<PatientSurveyAnswer, Answers>();
                        Mapper.CreateMap<PatientQS, QuestionsAnswers>().ForMember(c=>c.Answers, m=>m.MapFrom(
                              q=>Mapper.Map<List<PatientSurveyAnswer>, List<Answers>>(q.Answers)
                            ));
                        QuestionsAnswers q2 = Mapper.Map<QuestionsAnswers>(quesans);
var quesans=(来自db.patientserveyQuestions中的c
从db.PatientserveyQuestionResponses中的v开始
其中c.QuestionID==v.QuestionID&&c.PointID==PointID
选择新建{c,ans=c.PatientserveyQuestionAnswers。选择(n=>n.PatientserveyAnswer)}.ToList()。选择(c=>new PatientsQS{Question=c.c,Answers=c.ans.ToList())。ToList();
CreateMap();
Mapper.CreateMap().formMember(c=>c.Answers,m=>m.MapFrom(
q=>Mapper.Map(q.Answers)
));
QuestionsAnswers q2=Mapper.Map(quesans);
但是automapper给了我一个错误

缺少类型映射配置或不支持的映射

映射类型: 列出
1->问题答案
System.Collections.Generic.List
1[[PatientService1.PROP.PatientQS,PatientService1,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]->PatientService1.PROP.QuestionsAnswers

目标路径: 提问者

源值: System.Collections.Generic.List`1[PatientService1.PROP.PatientQS]


我是做错了还是真的不受支持?

您没有提供从列表到
问题的映射。另外,为什么您的<代码> CreateMap <代码>在查询的中间调用?它不是查询和映射的一部分,您的代码< CreateMap > /COD>在“代码> QuestionsAnswers < /COD> >和列表之间映射?我在那里看不到..QuestionsAnswers本身就是一个列表,映射就是在那里专门映射答案列表属性yun linq代码,直到找到引发异常的行。然后修理它。然后重新编写linq代码。