将值添加到基于模型的List

5 .net c# list generic-list

Backgournd

我目前正在尝试将一个值添加到模型列表中,但它似乎与我在JavaScript中的方式不同.

模型

public class ContentBase
{

    public string Name { get; set; }    
    public string Description { get; set; }
    public string LastModifiedTime { get; set; }
    public string KeyWords { get; set; }
    public string Content { get; set; }
    public string UniqueId { get; set; }
    public string library { get; set; }
    public string ContentSummary { get; set; }        
    public string[] images { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

功能增加价值

 List<ContentBase> returnList = new List<ContentBase>();
 foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img"))
  {
    HtmlAttribute att = img.Attributes["src"];
    returnList.Add(images = att.Value);  << this is wrong, just trying to give an idea of my intentions
  }
Run Code Online (Sandbox Code Playgroud)

我试图将结果添加 HtmlAttribute att = img.Attributes["src"];string[] images我的列表中.这样一旦我完成这个迭代,我将能够获得我从string[] images列表中的每个循环得到的所有值

更新

这是填充列表中其他项的上述函数

string content = "";
if (includeContent == true)
{
    content = rslt[ContentBase.fastContent] != null ? rslt[ContentBase.fastContent].ToString() : "";
}

returnList.Add(new ContentBase()
{
    UniqueId = rslt[ContentBase.fasstUniqueId] != null ? rslt[ContentBase.fasstUniqueId].ToString() : "",
    LastModifiedTime = rslt[ContentBase.fasstLastModifiedTime] != null ? rslt[ContentBase.fasstLastModifiedTime].ToString().Substring(0, 8) : "",
    Name = rslt[ContentBase.fastName] != null ? rslt[ContentBase.fastName].ToString() : "",
    Description = rslt[ContentBase.fastDescription] != null ? rslt[ContentBase.fastDescription].ToString() : "",
    KeyWords = rslt[ContentBase.fastKeyWords] != null ? rslt[ContentBase.fastKeyWords].ToString() : "",
    ContentSummary = rslt[ContentBase.fasstContentSummary] != null ? rslt[ContentBase.fasstContentSummary].ToString() : "",
    Content = content,

});
Run Code Online (Sandbox Code Playgroud)

Rez*_*aei 0

returnList是 a ,因此您应该向列表中List<ContentBase>添加一个新的。ContentBase

ContentBase对象包含一个string[] images属性,您可以在循环中或使用 linq 创建该属性并将其传递给新ContentBase创建的对象。您还应该提供创建的其他属性的值ContentBase。例如:

var returnList = new List<ContentBase>();
returnList.Add(new ContentBase()
{
    // Initialize properties the same way you are setting them
    // Including the images property this way:
    images = htmlDocument.DocumentNode.SelectNodes("//img")
                         .Select(x=>x.Attributes["src"].Value).ToArray()
});
Run Code Online (Sandbox Code Playgroud)