2015-07-21 46 views
-4

我試圖將項目添加到列表中(項目是不同類型的)。這是我如何做的。將不同類型的項目添加到.Net中的列表中

private List<JiraIssues> GetIssues(string result) 
    { 
     string x = null; 
     var summary = x; 
     List<JiraIssues> returnResulttoReport = new List<JiraIssues>(); 
     RootObject myresult = JsonConvert.DeserializeObject<RootObject>(result); 
     foreach (var item in myresult.issues) 
     { 
      Console.WriteLine(item.fields.summary); 
      summary = item.fields.summary; 
      foreach (var commentitem in item.fields.comment.comments) 
      { 
       var authorname = commentitem.author.name; 
       Console.WriteLine(commentitem.author.name); 
      } 

     } 

     /*var vnlist = (from up in spcall 
         where up.Caption == "Contacted" 
         select new JiraIssues 
         { 
          summary, 
          authorname, 
          ActualDate, 
          Value = up.Value 
         }).ToList();*/ 
     returnResulttoReport.AddRange(summary,authorname); 
     return returnResulttoReport ; 
    } 

獲取錯誤;

AddRange不能帶2個參數。

爲了更好的可視性我在這裏把我的JiraIssues:

[DataContract] 
public class JiraIssues 
{ 
    public JiraIssues() 
    { 
     Comments = new List<JiraComment>(); 
     //Components = new List<JiraComponent>(); 
    } 

    [DataMember(Order = 0)] 
    public string IssueKey; 

    [DataMember(Order = 1)] 
    public string Id; 

    [DataMember(Order = 2)] 
    public string Description; 

    [DataMember(Order = 3)] 
    public string Assignee; 

    [DataMember(Order = 4)] 
    public DateTime? Created; 

    [DataMember(Order = 5)] 
    public DateTime? Duedate; 

    [DataMember(Order = 6)] 
    public string Environment; 

    [DataMember(Order = 7)] 
    public string Priority; 

    [DataMember(Order = 8)] 
    public string Project; 

    [DataMember(Order = 9)] 
    public string Reporter; 

    [DataMember(Order = 10)] 
    public string Resolution; 

    [DataMember(Order = 11)] 
    public string Status; 

    [DataMember(Order = 12)] 
    public string Summary; 

    [DataMember(Order = 13)] 
    public string IssueType; 

    [DataMember(Order = 14)] 
    public DateTime? Updated; 

    [DataMember(Order = 15)] 
    public long? Votes; 

    [DataMember(Order = 16)] 
    public string Components; 

    [DataMember(Order = 17)] 
    public List<JiraComment> Comments; 
} 

[DataContract] 
public class JiraComment 
{ 
    [DataMember(Order = 0)] 
    public string CommentBody; 

    [DataMember(Order = 1)] 
    public string CommentAuthor; 

    [DataMember(Order = 2)] 
    public DateTime? CommentCreated; 

    [DataMember(Order = 3)] 
    public string CommentUpdateAuthor; 

    [DataMember(Order = 4)] 
    public DateTime? CommentUpdated; 

    [DataMember(Order = 5)] 
    public string CommentId; 

    [DataMember(Order = 6)] 
    public string CommentGroupLevel; 

    [DataMember(Order = 7)] 
    public string CommentParentKeyId; 

    [DataMember(Order = 8)] 
    public string CommentParentId; 
} 

我怎樣才能解決售後服務做到這一點?我需要添加並返回方法中的所有字符串。

+0

是'Summary'和'AuthorName'型與'JiraIssues'都兼容嗎? –

+5

['列表 .AddRange'](https://msdn.microsoft.com/en-us/library/z883w3dc%28v=vs.110%29.aspx)的文檔在我看來很清楚。如何將它們添加到不同的'AddRange'中?但是'summary' _already_看起來像'string'。考慮改變你的邏輯。目前還不清楚你想要做什麼。 –

+0

您已將returnResulttoReport聲明爲JiraIssues的列表。您可以添加到該列表中的唯一類型是JiraIssues。 AddRange()方法就像Add()方法,只是它將JiraIssues列表添加到列表中。 – HaukurHaf

回答

2

有幾個問題。首先,致電AddRange。這需要一個枚舉的,像一個數組:

returnResulttoReport.AddRange(new string[] { summary, authorname }); 

其次,最重要的是,列表,List<JiraIssues>的類型,與您添加(的string型)的項目不兼容。你不能那樣做。這意味着上面的代碼將無法正常工作...您可以更改列表的類型爲List<object>,但這會使其不強烈類型,我不建議這樣做。

相反,您應該找到一種方法將字符串轉換爲JiraIssues實例,或者將其放入另一個類型的列表中。

假設JiraIssuesSummaryAssignee財產,那麼你可以做:

returnResulttoReport.Add(new JiraIssues() { Summary = summary, Assignee = authorname }); 
+0

我修改了我的問題以獲取清晰的圖片。 – AskMe

+0

@TryingBest:稍微修改一下代碼。 –

+0

如果有些用戶覺得如此,有些用戶會覺得如此。不要太擔心。 –

相關問題