LINQ
このオブジェクトの中に新しいtwoWords
オブジェクトを選択しList
、関数/メソッドを呼び出して値を設定するために使用しています。
これが理にかなっているかどうかを確認してください。私はそれを大幅に簡略化しました。私は本当にlinqステートメントを使いたいですfrom
select
。
の最初の関数は機能しGOGO
、2番目の関数は失敗します(ただし、同じタスクは実行されません)
// 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();
}
}
の2番目の関数はGOGO
エラーを引き起こします:
select句の式のタイプが正しくありません。'Select'の呼び出しで型推論が失敗しました。
私の質問は、関数を使用して値を設定しながら、上記の句で新しいtwoWords
オブジェクトを選択するにはどうすればよいですか?from
setvalues