ยังไงจะทำการเจอรายการคำนค่าที่ครั้งหนึ่งใน.อข่าย?

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.

@เช่ดีค่ะ มี ForEach ฟังก์ชันแต่ภายในทำนองเดียวกันกับน้อยกว่าการแสดงและ legibility:S
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);

เห็นได้ชัดว่าคุณกำลังจะต้องการจะทำการประมวลผล(ตั้งคำที่สอดคล้องคงว่างเปล่าใน"เจอตั้ง"ระหว่างการสอบส,etc),แต่นั่นน่าจะถึงตาคุณเริ่มผ่าได้แล้ว

2021-11-23 21:42:34

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่