ฉันมีเรียนเป็นเรื่อง:
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?