2016-07-14 94 views
5

backgourndAggiunta di un valore ad una lista che si basa su un modello

Attualmente sto cercando di aggiungere un valore a un elenco di un modello però sembra essere diverso da come lo farei in JavaScript .

Modello

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

funzione per aggiungere valore

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 
    } 

Domanda

Sto cercando di aggiungere il risultato da HtmlAttribute att = img.Attributes["src"]; in string[] images della mia l ist. In questo modo una volta che completano questa iterazione sarò in grado di avere tutti i valori che ottengo dalla mia per ogni loop nella string[] images parte della mia lista

Aggiornamento

Questa è la funzione che precede che popola la altre voci nella mia lista

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, 

}); 
+0

Cercando di aggiungere att.Value alla proprietà Images interno della lista ritorno –

+0

Sarebbe lista contenere solo un singolo 'elemento ContentBase' cui è possibile impostare la proprietà' images'? –

+0

Era via, sì conterrà gli elementi che vengono ricevuti dal mio per ogni ciclo. Il conteggio degli elementi sarà dinamico non statico –

risposta

0

il returnList è un List<ContentBase>, così si dovrebbe aggiungere un nuovo ContentBase al elenco.

L'oggetto ContentBase contiene una proprietà string[] images cui è possibile creare nel ciclo o utilizzando LINQ e passarlo al nuovo ContentBase creato. Dovresti anche fornire il valore di altre proprietà create ContentBase. Per esempio:

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() 
}); 
1

Qui il returnList è una lista di ContentBase così i suoi elementi dovrebbero essere gli oggetti della classe corrispondente. Quindi, il codice per aggiungere elementi a questa sarà come la seguente:

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

Si può solo creare un oggetto ContentBase prima e poi aggiungerlo:

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); 
    } 
+1

Penso che 'base.image = att.value;' sarà sbagliato poiché 'att.Value' sarà una stringa e' base.image' sarà una stringa di array –

1

È possibile utilizzare Linq senza nemmeno utilizzare il foreach loop, in questo modo:

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