如何设置的所有发现清单词值一次。净?

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 

}

所以我怎么会组wordIsFound1时在第1次和第3项中的列表(那匹配的字谜?

.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.

谢谢为答复,我想有可能以更优雅的方式,比循环。 例如,行:``if(单词.存在(x=>x。wordTxt==字词搜索)){``....检查所有的词语没有它是一个循环。 我知道幕后它为循环,但我认为可能会有类似的代码,用于设置。 我猜测不。
E.D.

@E.D。是的。 有一个ForEach功能,但国内不同的性能较小的和易读性:S
Leandro Bardelli
0

更简单的方法可能是使用。你的类型(或甚至只是一个 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ý
..................................................................................................................