कैसे स्थापित करने के लिए सभी पाया की सूची शब्दों के मूल्यों पर एक बार में .शुद्ध?

0

सवाल

मैं एक वर्ग के रूप में इस तरह:

    public class cls_words : IEquatable<cls_words>
    {
        public int indx { get; set; }
        public string wordTxt { get; set; }
        public int wordIsFound { get; set; }

        public override string ToString()
        {
            return "ID: " + wordIsFound + "   Name: " + wordTxt;
        }
        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            cls_words objAsWord = obj as cls_words;
            if (objAsWord == null) return false;
            else return Equals(objAsWord);
        }
        public override int GetHashCode()
        {
            return wordIsFound;
        }

        public bool Equals(cls_words other)
        {
            if (other == null) return false;
            return (this.wordIsFound.Equals(other.wordIsFound));
        }
    }

मूल रूप से वर्ग एक शब्द है, और चाहे या नहीं यह पाया गया है में एक खोज.

तो मैं एक सूची बनाने के इस वर्ग के रूप में इस तरह:

List<cls_words> wordsIn = new List<cls_words>();

wordsIn.Add(new cls_words { indx= 1, wordTxt = "test", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 2, wordTxt = "the", wordIsFound=0 });
wordsIn.Add(new cls_words { indx= 3, wordTxt = "test", wordIsFound=0 });

फिर जब मैं खोज की सूची देखने के लिए अगर यह होता है, एक शब्द में, मैं सेट करना चाहते हैं सभी wordIsFound मान 1 करने के लिए जहां उपयुक्त हो । कुछ शब्दों की सूची में एक ही हो सकता है.

तो कुछ की तरह

string wordSearch = "test";

if (wordsIn.Exists(x => x.wordTxt == wordSearch)) {

   //set all wordIsFound = 1 where word matches wordSearch 

}

तो कैसे मैं सेट wordIsFound करने के लिए 1 पर 1 और 3 में आइटम की सूची (है कि लोगों को मैच के wordSearch ?

.net c# list match
2021-11-23 21:30:19
2
0
string wordSearch = "test";
 foreach (cls_words clsword in wordsIn) {
   if(cls_word.wordTxt == wordSearch) {
      clsword.wordIsFound = 1;
   }
}
2021-11-23 21:35:17

है कि सबसे अच्छा है/केवल तरीका है? एक पाश में?
E.D.

thx के लिए जवाब है, मैंने सोचा था कि वहाँ हो सकता है एक और अधिक सुरुचिपूर्ण तरीके से एक पाश. उदाहरण के लिए, लाइन: `` यदि (wordsIn.मौजूद है(x => x.wordTxt == wordSearch)) { `` ....चेक सभी शब्दों के बिना यह जा रहा है एक पाश. मुझे पता है कि पर्दे के पीछे यह पाश करने के लिए है, लेकिन मैंने सोचा था कि वहाँ हो सकता है इसी तरह के कोड की स्थापना के लिए. मुझे लगता है कि नहीं.
E.D.

@E. D. हाँ । वहाँ है एक ForEach समारोह, लेकिन आंतरिक के साथ ही कम प्रदर्शन और स्पष्टता:एस
Leandro Bardelli
0

आसान दृष्टिकोण हो सकता है का उपयोग करने के लिए एक HashSet के साथ अपने प्रकार (या यहां तक कि सिर्फ एक string).

HashSet<string> foundWords = new HashSet<string>();
HashSet<string> allWords = new HashSet<string>(); 
allWords.Add("test"); 
allWords.Add("apple");
allWords.Add("test"); // This will just "fail silently" if the word is already in the allWords HashSet.

// then to test:
string wordSearch = "test";
if (allWords.Contains(wordSearch)) foundWords.Add(wordSearch);

// you can even loop over your input of a very big string with something like:
string bigString = "this is a long string with many words";
int maxWords = 1000;
string[] allWordsToTest = bigString.Split(' ', maxWords);
foreach (string s in allWordsToTest)
    if (allWords.Contains(s)) foundWords.Add(s);

जाहिर है आप चाहते करने जा रहे हैं क्या करने के लिए कुछ प्रसंस्करण (सेट करने के लिए शब्दों का एक सुसंगत मामले में, खाली है "पाया" सेट के बीच परीक्षण, आदि), लेकिन यह है कि आप शुरू हो जाना चाहिए.

2021-11-23 21:42:34

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में