将两个 List 存储<strings>到集合对象的属性中 c#



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

新增功能我正在从网页上抓取 html 内容,并尝试将内容存储在使用如下所示的 SpecialGames 模型定义的集合中,这两个属性值首先存储在 2 个字符串列表中,1 个用于图像属性,1 个用于正文属性,然后我想插入这两个列表作为 SpecialGames 集合中属性的值。

型号类别(特价):

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;

            }

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

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

最新更新