Sto utilizzando LINQ
per selezionare un nuovo oggetto twoWords
in un List
di questi oggetti e impostare i valori chiamando una funzione/metodo.LINQ selezionare il nuovo oggetto, impostare i valori dell'oggetto nella funzione
Si prega di vedere se questo ha senso, l'ho semplificato molto. Voglio davvero usare le istruzioni linq from
select
.
La prima funzione in GOGO
funziona, il secondo fallisce (non svolgono lo stesso compito però)
// simple class containing two strings, and a function to set the values
public class twoWords
{
public string word1 { get; set; }
public string word2 { get; set; }
public void setvalues(string words)
{
word1 = words.Substring(0,4);
word2 = words.Substring(5,4);
}
}
public class GOGO
{
public void ofCourseThisWillWorks()
{
//this is just to show that the setvalues function is working
twoWords twoWords = new twoWords();
twoWords.setvalues("word1 word2");
//tada. object twoWords is populated
}
public void thisdoesntwork()
{
//set up the test data to work with
List<string> stringlist = new List<string>();
stringlist.Add("word1 word2");
stringlist.Add("word3 word4");
//end setting up
//we want a list of class twoWords, contain two strings :
//word1 and word2. but i do not know how to call the setvalues function.
List<twoWords> twoWords = (from words in stringlist
select new twoWords().setvalues(words)).ToList();
}
}
La seconda funzione di GOGO
causerà un errore:
L' il tipo dell'espressione nella clausola select non è corretto. Digitare un'inferenza non riuscita nella chiamata a 'Seleziona'.
La mia domanda è, come faccio a selezionare il nuovo oggetto nella twoWords
sopra from
clausola, mentre l'impostazione dei valori utilizzando la funzione setvalues
?
Per inciso, * davvero * aiuta la leggibilità se si seguono le convenzioni di denominazione .NET anche per un semplice codice di esempio . –