【问题标题】:Adding a value to a List which is based on a Model向基于模型的列表添加值
【发布时间】:2016-07-14 16:01:44
【问题描述】:

背景

我目前正在尝试向模型列表中添加一个值,但这似乎与我在 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 每个循环中获得的所有值

更新

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

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,

});

【问题讨论】:

  • 试图将 att.Value 添加到返回列表内的 Images 属性中
  • 列表是否只包含一个您设置其images 属性的ContentBase 元素?
  • 离开了,是的,它将包含从我的每个循环接收到的元素。元素计数将是动态的而不是静态的
  • for循环只能提供属于单个ContentBaseimages。其他ContentBase 对象来自哪里?
  • 尽管我可能没有答案,但我认为一次推送所有项目可能是一个好主意,如果不是,您将获得仅包含图像 Url 的列表项目

标签: c# .net list generic-list


【解决方案1】:

这里的returnListContentBase 的列表,所以它的项目应该是相应类的对象。所以添加项目的代码如下:

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}}
  }

【讨论】:

    【解决方案2】:

    您可以先创建一个ContentBase 对象,然后添加它:

     List<ContentBase> returnList = new List<ContentBase>();
     foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img"))
      {
        HtmlAttribute att = img.Attributes["src"];
        ContentBase base = new ContentBase();
        base.image = att.value;
        returnList.Add(base);
      }
    

    【讨论】:

    • 我认为base.image = att.value; 会出错,因为att.Value 将是一个字符串,而base.image 将是一个字符串数组
    【解决方案3】:

    您甚至可以不使用foreach 循环来使用Linq,如下所示:

    List<ContentBase> returnList = htmlDocument.DocumentNode.SelectNodes("//img")
         .Select(img => new ContentBase {images = new[] {img.Attributes["src"].Value}}).ToList();
    

    【讨论】:

      【解决方案4】:

      returnListList&lt;ContentBase&gt;,因此您应该将新的 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()
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-15
        • 2021-08-05
        • 2014-08-06
        • 2021-04-11
        • 2017-08-06
        • 1970-01-01
        相关资源
        最近更新 更多