【问题标题】:Storing two List<strings> into the properties of a collection object issue c#将两个 List<strings> 存储到集合对象问​​题 c# 的属性中
【发布时间】:2015-09-05 15:20:45
【问题描述】:

您好,我正在尝试将已填充的两个列表存储到集合对象中,该集合名为“SpecialOffers”,我需要将第一个字符串列表设置为属性之一,将另一个字符串列表设置为其他,但是我在将这些列表插入集合以在集合中创建新的 SpecialOffers 对象时遇到问题。任何帮助将不胜感激。

新 我正在从网页上抓取 html 内容,并尝试将内容存储在使用下面看到的 SpecialOffers 模型定义的集合中,这两个属性值首先存储在 2 个字符串列表中,1 个用于 image 属性,1 个用于 body 属性,我然后想插入这两个列表作为 SpecialOffers 集合中属性的值。

模型类(SpecialOffers):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing.Imaging;

namespace RedSeaExample.Models
{
    public class SpecialOffers
    {
        public SpecialOffers(string image, string body)
        {
            this.image = image; ;
            this.body = body;
        }
               public string image { get; set; }
               public string body {get; set;}

     }
}
**Class to retrieve image sources and table html source**

public Collection<Models.SpecialOffers> captureLinks(string source, Collection<Models.SpecialOffers> specialOffersCollection, HtmlDocument html)
            {
                 int i = 0;
                 List<string> bodyList = new List<string>();
                 List<string> getString = new List<string>();

                List<List<string>> table1 = html.DocumentNode.SelectSingleNode("//table[1]").Descendants("tr").Skip(1).Where(tr => tr.Elements("td").Count() > 1).
                Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim()).ToList()).ToList();

                 var table = html.DocumentNode.SelectNodes("//table[1]").FirstOrDefault();


                         foreach (List<string> txt in table1)
                         {
                             foreach (string example in txt)
                             {                        
                                getString.Add(example + (i + 1).ToString());


                             }
                             bodyList.AddRange(table.SelectNodes("..//img/@src").Select(t => t.OuterHtml + (i + 1).ToString()));

                             specialOffersCollection.Add(bodyList);
                                 //(new SpecialOffers(bodyList.ToString(), getString.ToString()));
                         }


                         return specialOffersCollection;



            }

【问题讨论】:

  • 当您发布代码@Rob 时,请您执行以下操作以显示所有相关代码和/或对象/变量的声明,并且不要发布部分代码.. 您熟悉如何创建列表 加载它并循环它..?如果不是这里是一个很好的链接,可以帮助你dotnetperls.com/list,也许specialOffersCollection 应该是一个类,然后你应该从那里创建一个 List 作为一个类
  • 您需要包含复制问题的完整代码,主要问题是没有显示类型是什么:table1、getString、bodyList、specialOffersCollection。此外,目前还不清楚您要实现的目标
  • 您看到的错误是什么?我们无法为您调试。
  • 你有什么问题?
  • 如何将两个字符串列表插入到 specialOffersCollection 对象中以设置属性、图像和正文

标签: c# string visual-studio-2010 list collections


【解决方案1】:

我不完全确定我是否理解您想要完成的任务,但您可以使用 .Zip() 从并行列表中构建特别优惠列表。

specialOffersCollection.AddRange(
    bodyList.Zip(getString, (b, g) => new SpecialOffers(b, g));

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 2013-02-13
    • 1970-01-01
    • 2022-01-09
    • 2014-09-19
    • 1970-01-01
    • 2019-10-01
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多