简体   繁体   English

存储两个列表<strings>进入集合对象的属性问题 c#

[英]Storing two List<strings> into the properties of a collection object issue c#

Hi I am trying to store two List that have been populated already, into collection objects, the collection is named "SpecialOffers" and I require the first string list to be set as one of the properties and the other string list as the other, however I am having issues inserting these lists into the collection to create new SpecialOffers objects in the collection.嗨,我正在尝试将两个已填充的列表存储到集合对象中,该集合名为“SpecialOffers”,我需要将第一个字符串列表设置为属性之一,将另一个字符串列表设置为另一个,但是我在将这些列表插入到集合中以在集合中创建新的 SpecialOffers 对象时遇到问题。 Any help would be greatly appreciated.任何帮助将不胜感激。

NEW I am scraping html content off a webpage, and trying to store the content in a collection defined using the SpecialOffers model seen below, both properties values are first stored in 2 string lists, 1 for the image property and 1 for the body property, I would then like to then insert these two lists as the values for the properties in the SpecialOffers collection.新我正在从网页上抓取 html 内容,并尝试将内容存储在使用下面看到的 SpecialOffers 模型定义的集合中,两个属性值首先存储在 2 个字符串列表中,1 个用于图像属性,1 个用于 body 属性,然后我想插入这两个列表作为 SpecialOffers 集合中属性的值。

Model Class (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;



            }

I'm not totally sure I understand what you're trying to accomplish, but you can construct a list of special offers from parallel lists using .Zip() .我不完全确定我了解您要完成的任务,但是您可以使用.Zip()从并行列表中构建一个特别优惠列表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM