简体   繁体   English

使用 Linq 更新 List 的对象

[英]Update a List's object using Linq

I would like to assign an object in a list using Linq, but I can't find how to do it.我想使用 Linq 在列表中分配一个对象,但我找不到如何去做。 I tried this:我试过这个:

Items.Where(i => i.Code == line.Code).Single() = line;

But it seems I simply cannot give the statement an object.但似乎我根本无法给该语句一个对象。 I can modify properties of those objects (Like this) :我可以修改这些对象的属性(像这样):

Items.Where(i => i.Code == line.Code).Single().name = line.name;

But giving it the full item doesn't seem to be accepted.但给它完整的项目似乎不被接受。 Is there a reason to prevent this / a way to assign my value to the compatible item ?是否有理由阻止这种/一种将我的价值分配给兼容项目的方法? Thank you for your answer.谢谢您的回答。

You cannot replace the value in the list using LINQ, because LINQ is a projection of a collection.不能使用 LINQ 替换列表中的值,因为 LINQ 是集合的投影。

You can get your item and then find it in the list using IndexOf :您可以获取您的项目,然后使用IndexOf在列表中找到它:

var item = Items.Single(i => i.Code == line.Code);
int index = Items.IndexOf(item);
Items[index] = new Item(); // replace the value

However, this approach is far from optimal in terms of algorithmic complexity, because it has to read through your list multiple times.但是,这种方法在算法复杂性方面远非最佳,因为它必须多次通读您的列表。
You can try to read more on Dictionary to find, add or replace items by key (in your case, Code ) efficiently.您可以尝试在Dictionary上阅读更多内容,以高效地按键(在您的情况下为Code )查找、添加或替换项目。

From MSDN Documentation: Language Integrated Query来自 MSDN 文档:语言集成查询

By using query syntax, you can perform filtering, ordering, and grouping operations on data sources with a minimum of code.通过使用查询语法,您可以用最少的代码对数据源执行过滤、排序和分组操作。

You can not perform assignment using LINQ , you need to use index to update particular record from the list您不能使用 LINQ 执行分配,您需要使用索引来更新列表中的特定记录

To update record you can try FindIndex()要更新记录,您可以尝试FindIndex()

int index = Items.FindIndex(x => x.Code == line.Code); //returns -1 if record not found
Items[index]?.Name = "Stackoverflow";

Think about what the Single method returns.想想Single方法返回什么。 It returns a reference to the object that much the predicate you pass in the Where method.它返回对对象的引用,该引用与您在Where方法中传递的谓词相同。 Specifically, when there is only one item in the Items that pass the predicate in the Where method, you will get a reference to this object.具体来说,当Items中只有一项传递Where方法中的谓词时,您将获得对该对象的引用。 If there are more than one items, you will get an exception and if there are no items you will get null.如果有多个项目,您将获得异常,如果没有项目,您将获得空值。 Let's think about the happy case, in which you will have only one item.让我们考虑一下快乐的情况,在这种情况下,您将只有一件物品。 In that case, essentially you try to assign to this reference a new object.在这种情况下,本质上您尝试为该引用分配一个新对象。 This does not make any sense.这没有任何意义。 On the other hand that you try afterwards, makes sense, holding the reference you can mutate its state.另一方面,您之后尝试是有道理的,持有引用您可以改变其状态。

Single() is a method that returns a value. Single()是一种返回值的方法。 You can't usually assign to the return from an expression.您通常不能分配给表达式的返回值。 It is ultimately no different to saying:最终与说:

Math.Max(32, 42) = 14;

at which point, Max is going to say "um, no; I was telling you the answer - I wasn't expecting you to tell me".在这一点上, Max会说“嗯,不;我在告诉答案——我没想到你会告诉我”。


In theory, what you want here is possible, but only in advanced scenarios involving ref -returns.理论上,你想要的在这里可能的,但只在涉及ref -returns 的高级场景中。 LINQ does not deal with ref -returns. LINQ 不处理ref -returns。

下面的 linq 会满足您的要求吗?

      items.Where(i => i.Code== line.code)?.ToList()?.ForEach(i => i.name= line.name);

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

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