2017-04-08 45 views
0

最終,這只是一個基本的.net問題,但有一點背景:我試圖使用SQL Server Integration Services腳本組件從調查猴的rest API中提取數據。因爲這發生在SSIS中的腳本任務中,並且在GAC中註冊輔助程序庫不是一種選擇,所以第三方庫和第三方SSIS工具都不可用。顯然,這不是解決這個問題的理想方式,但這是我現在所堅持的。我可以將我想要的json數據拉出來,每個數據都被反序列化爲<SurveyDetail>類型的對象,並添加到該類型的列表中。類:將使用linq進行反序列化的json變成「非規範化」列表

public class SurveyDetail 
     { 
      public long? Id { get; set; } 
      public string Title { get; set; } 
      public string Nickname { get; set; } 
      public string Category { get; set; } 
      public string Language { get; set; } 
      public int? Question_Count { get; set; } 
      public DateTime? Date_Created { get; set; } 
      public DateTime? Date_Modified { get; set; } 
      public List<Page> Pages { get; set; }    
     } 

public class Page 
     { 
      public List<Question> Questions { get; set; } 
     } 

public class Question 
     { 
      public long? Id { get; set; } 
      public string Title { get; set; } 
      public QuestionFamily? Family { get; set; } 
      public List<Headings> Headings { get; set; } 
      public QuestionAnswers Answers { get; set; } 
     } 

public class Headings 
     { 
      public string Heading { get; set; } 
     } 

public class QuestionAnswers 
     { 
      public List<Choice> Choices { get; set; } 
     } 

public class Choice 
     { 
      public long? Id { get; set; } 
      public string Text { get; set; }  
     } 

所以給予List<SurveyDetail>,我想把它壓平成List<SurveyQuestionOption>爲便於分期到數據庫的:

public class SurveyQuestionOption 
    { 
     public long? SurveyID { get; set; } 
     public string SurveyTitle { get; set; } 
     public DateTime? DateCreated { get; set; } 
     public DateTime? DateModified { get; set; } 
     public long? QuestionID { get; set; } 
     public string QuestionTitle { get; set; } 
     public string QuestionType { get; set; } 
     public string QuestionText { get; set; } 
     public long? AnswerOptionID { get; set; } 
     public string AnswerOptionText { get; set; } 
    } 

的絆腳石似乎是一個事實,即<QuestionAnswers>對象<Question>有時可能爲空,而我的目標是在這種情況下產生空值,但在嘗試執行此操作時收到空引用異常:

List<SurveyQuestionOption> flatData = new List<SurveyQuestionOption>(); 

    flatData = (from s in surveyDetails 
        from p in s.Pages 
        from q in p.Questions 
        from h in q.Headings.DefaultIfEmpty() 
        from c in q.Answers.Choices.DefaultIfEmpty() 
        select new SurveyQuestionOption 
        { 
         SurveyID = s.Id == null ? null : s.Id, 
         SurveyTitle = s.Title == null ? null : s.Title, 
         DateCreated = s.Date_Created == null ? null : s.Date_Created, 
         DateModified = s.Date_Modified == null ? null : s.Date_Modified, 
         QuestionID = q.Id == null ? null : q.Id, 
         QuestionTitle = q.Title == null ? null : q.Title, 
         QuestionType = q.Family.Value.ToString(), 
         QuestionText = h.Heading.FirstOrDefault().ToString() == null ? null : h.Heading.FirstOrDefault().ToString(), 
         AnswerOptionID = c.Id == null ? null : c.Id, 
         AnswerOptionText = c.Text == null ? null : c.Text 
        }).ToList<SurveyQuestionOption>(); 

我知道類似的問題已經被問到,但我仍然努力爲我的特定情況獲得正確的查詢。我顯然不是交易的.net開發者。任何幫助非常感謝。

回答

0

我能夠通過爲<Question>型爲空的成員明確的構造函數來解決這個問題,併爲DeafultIfEmpty像這樣提供的<Choice>一個新實例:

public class Question 
{ 
    public long? Id { get; set; } 
    public string Title { get; set; } 
    public QuestionFamily? Family { get; set; } 
    public List<Headings> Headings { get; set; } 
    public string Heading { get; set; } 
    public bool? Visible { get; set; } 
    public bool? ForcedRanking { get; set; } 
    internal string Href { get; set; } 
    public QuestionAnswers Answers { get; set; } 
    public string Nickname { get; set; } 

    public Question() 
    { 
     Answers = new QuestionAnswers(); 
    } 
} 

public class QuestionAnswers 
{ 
    public List<Choice> Choices { get; set; } 
    public OtherAnswer Other { get; set; } 

    public QuestionAnswers() 
    { 
     Choices = new List<Choice>(); 
    } 
} 



flatData = (from s in surveyDetails 
        from p in s.Pages 
        from q in p.Questions 
        from h in q.Headings.DefaultIfEmpty() 
        from c in q.Answers.Choices.DefaultIfEmpty(new Choice()) 
        select new SurveyQuestionOption 
        { 
         SurveyID = s.Id == null ? null : s.Id, 
         SurveyTitle = s.Title == null ? null : s.Title, 
         DateCreated = s.Date_Created == null ? null : s.Date_Created, 
         DateModified = s.Date_Modified == null ? null : s.Date_Modified, 
         QuestionID = q.Id == null ? null : q.Id, 
         QuestionTitle = q.Title == null ? null : q.Title, 
         QuestionType = q.Family.Value.ToString(), 
         QuestionText = h.Heading.FirstOrDefault().ToString() == null ? null : h.Heading.FirstOrDefault().ToString(), 
         AnswerOptionID = c.Id == null ? null : c.Id, 
         AnswerOptionText = c.Text == null ? null : c.Text 
        }).ToList<SurveyQuestionOption>();