简体   繁体   English

C# LINQ 更新项列表<string>

[英]C# LINQ update item List<string>

I have problem with updating a single item under List<string> that matches a different string using LINQ.我在更新List<string>下的单个项目时遇到问题,该项目使用 LINQ 匹配不同的字符串。 Let's say that I have a list of names and I want to check if name "John" already exists in my list.假设我有一个名字列表,我想检查名字“John”是否已经存在于我的列表中。 If yes, then replace "John" with "Anna".如果是,则将“John”替换为“Anna”。

Here is what I do:这是我所做的:

var sItem = myList.First(n=> n == "John"); //I am 100% sure that John exists, that\s why I use .First
sItem = "Anna";

This is how it should work, but when I check my List (myList) after the process, the original item is still there (I can still see John, instead of Anna).这是它应该如何工作,但是当我在此过程后检查我的列表 (myList) 时,原始项目仍然存在(我仍然可以看到约翰,而不是安娜)。 I also tried to implement INotifyChanged on the List, but still no result.我也尝试在List上实现INotifyChanged,但是还是没有结果。

What am I doing wrong?我究竟做错了什么?

If you need to update, use FindIndex : 如果需要更新,请使用FindIndex

int index = myList.FindIndex(n => n == "John");
myList[index] = "Anna";

You are assigning the result of linq query to a string variable. 您将linq查询的结果分配给字符串变量。 That is not the element of list but a variable that is also referencing the element of that list. 这不是列表的元素,而是一个也引用该列表元素的变量。 Changing the value of variable sItem will define a new string that will be referenced by the sItem and the item in the list remains unchanged. 更改变量sItem的值将定义将由sItem引用的新字符串,并且列表中的项保持不变。

You can use FindIndex to get the index of element in the array and use it to refer to list element. 您可以使用FindIndex获取数组中元素的索引,并使用它来引用list元素。

int index = myList.FindIndex(n => n == "John");
myList[index] = "Anna";

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List. 搜索与指定谓词定义的条件匹配的元素,并返回整个List中第一个匹配项的从零开始的索引。

Edit 编辑

When one string variable is assigned to other. 将一个字符串变量分配给其他变量时。 They both would be referencing the same string but when you assign a different string to second variable for instance then they both referencing different strings. 它们都将引用相同的字符串,但是当您为第二个变量分配不同的字符串时,它们都引用不同的字符串。 See the following example from answer of Eric Lippert. 请参阅Eric Lippert的回答中的以下示例。

a----------------------Hello

Then you say that "b = a", which means attach another piece of string to the same thing that a is attached to : 然后你说,“B = A”,这意味着另附一根细绳同样的事情a连接到

a----------------------Hello
                      /
b---------------------

Then you say "now attach b to Hi" 然后你说“现在把b加到嗨”

a----------------------Hello

b----------------------Hi
int index = strList.FindIndex(n => n == "John");
if (index != -1)
{
    strList[index] = "Anna";
}

This will ensure that if "John" does not exist in the list, the program does not crash.这将确保如果列表中不存在“John”,则程序不会崩溃。

It should work for you 它应该适合你

List<string> list = new List<string>(); 

list.Add("Gandarez");
list.Add("Carlos");

var search = list.FirstOrDefault(l => l == "Carlos");

if (search != null)
{
    var index = list.IndexOf("Carlos");
    list.RemoveAt(index);
    list.Insert(index, "Test");
}
int sItem = myList.FindIndex(x => x == "John");
myList[sItem] = "Anna";
static void Main (string [] args)
    {
      List <int> list = new List <int> ();
      list.Add (1);
      list.Add (2);
      list.Add (3);      

list = list.Select (x => x + 6) .ToList ();    

}

The problem you are seeing is that System.String , while actually a reference type, acts like a value type. 您看到的问题是System.String实际上是一个引用类型,就像一个值类型。 So, when you assign a new value to sItem you are replacing it, not changing it. 因此,当您为sItem分配新值时,您将替换它,而不是更改它。

If you were using a true reference type, what you tried could have worked: 如果您使用的是真正的引用类型,那么您尝试过的方法可能有效:

List<Person> myList = ....;
var sItem = myList.First(p=> p.Name == "John");
sItem.Name = "Anna";

(Assigning -- sItem = new Person("Anna"); -- would still fail the same way,) (分配sItem = new Person("Anna"); - 仍然会以同样的方式失败,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM