我在C#中有一个这样的列表,其中在某些行中,其中一个字段有时具有"()"
的条目:
我可以找到以下这些行:
var ps = phraseSources.Where(x => x.WatsonMeaning == "()");
但是如何使用Linq将WatsonMeaning列中所有出现的“()”更改为“ n / a”。
答案 0 :(得分:2)
只需使用ForEach
LINQ方法:
list
.Where(i => i.WatsonMeaning == "()")
.ToList()
.ForEach(i => i.WatsonMeaning = "n/a")
答案 1 :(得分:1)
尝试一下:
static void Test()
{
var phraseSources = new List<A>();
phraseSources.Add(new A { WatsonMeaning = "()" });
phraseSources.Add(new A { WatsonMeaning = "text1" });
phraseSources.Add(new A { WatsonMeaning = "()" });
phraseSources.Add(new A { WatsonMeaning = "text2" });
phraseSources.Add(new A { WatsonMeaning = "text3" });
phraseSources.Where(x => x.WatsonMeaning == "()")
.ToList()
.ForEach(x => x.WatsonMeaning = "n/a");
foreach ( var item in phraseSources )
Console.WriteLine(item.WatsonMeaning);
}
class A
{
public string WatsonMeaning;
}