c# - 向基于模型的列表添加值

标签 c# .net list generic-list

背景

我目前正在尝试向模型列表中添加一个值,但这似乎与我在 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; }
}

增值功能

 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
  }

问题

我正在尝试将 HtmlAttribute att = img.Attributes["src"]; 的结果添加到我列表的 string[] images 中。这样,一旦我完成此迭代,我将能够在我的列表的 string[] images 部分中获得我从 for each 循环中获得的所有值

更新

这是填充列表中其他项目的前面的函数

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,

});

最佳答案

这里的returnListContentBase 的List,所以它的项应该是相应类的对象。因此向其中添加项目的代码如下所示:

foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img"))
  {
    HtmlAttribute att = img.Attributes["src"];
    returnList.Add(new ContentBase(){library="some value",images = new string[]{att.value}}
  }

关于c# - 向基于模型的列表添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378996/

相关文章:

c# - 将 'LoadfromCollection' 与包含另一个列表的列表一起使用

c# - 如何通过输入和输出值估计数学函数?

c# - 在 .net 中处理大量 TCP 客户端连接

c# - 如何在 C# .net 中获取处理器 CPU 标志?

java - 在Java中将CSV文件读入数组[不兼容的类型: Integer cannot be converted to int[].]

php - 我如何设计类似于 Pinterest 图板的图像列表?

c# - TableLayoutPanel 显示垂直滚动

c# - 按位检查和相等检查哪个更有效?

c# - 使用 WCF 异步有好处吗?

c# - 如何使用 ASP.NET MVC 5.0 的 ASP.NET Identity 实现密码重置?