में ऑब्जेक्ट के मान सेट करना मैं LINQ
का उपयोग कर रहा हूं ताकि twoWords
ऑब्जेक्ट को List
में ऑब्जेक्ट्स में चुन सकें और फ़ंक्शन/विधि को कॉल करके मान सेट कर सकें।LINQ नई ऑब्जेक्ट का चयन करें, फंक्शन
कृपया देखें कि क्या यह समझ में आता है, मैंने इसे बहुत सरल बना दिया है। मैं वास्तव में linq स्टेटमेंट from
select
का उपयोग करना चाहता हूं।
GOGO
में पहली समारोह काम करेंगे, एक दूसरे में विफल रहता है
// 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();
}
}
GOGO
की दूसरी समारोह एक त्रुटि का कारण होगा (वे एक ही काम है, हालांकि प्रदर्शन नहीं करते):
चयनित खंड में अभिव्यक्ति का प्रकार गलत है। 'चयन' पर कॉल में अनुमान लगाया गया।
मेरा प्रश्न है, मैं ऊपर from
खंड में नई twoWords
वस्तु कैसे चयन करते हैं, setvalues
समारोह का उपयोग कर मूल्यों को सेट करते समय है?
एक तरफ, यह * वास्तव में * पठनीयता में मदद करता है यदि आप सरल नमूना कोड के लिए भी .NET नामकरण सम्मेलनों का पालन करते हैं । –