我想使用linq更新链表中的节点值。遍历列表,找到正确的节点并更新值有效,但我认为linq方法可以更清晰。
这是我尝试编译错误cannot convert lambda expression to type <Main.Globals.Node> because it is not a delegate type
:
// get IV value where Node BookID=4
var val = Globals.BookLL.Where(B => B.BookID == 4).Select(B => B.IV).Single();
// can update first node using this method
Globals.BookLL.First.Value.IV = 999;
// can upddate IV by traversing list
LinkedListNode<Globals.Node> Current = Globals.BookLL.First;
while (Current != null)
{
if(Current.Value.BookID==4)
{
Current.Value.IV = 444;
}
Current = Current.Next;
}
// how can you update IV using linq?
Globals.BookLL.Find(B => B.BookID == 4).Value.IV = 999; // cannot convert lambda expression to type <Main.Globals.Node> because it is not a delegate type
感谢您的帮助。
答案 0 :(得分:2)
如果您使用
Globals.BookLL val = Globals.BookLL.Single(B => B.BookID == 4);
要确定该项目,您可以使用Find()
Globals.BookLL.Find(val).Value.IV = 999;
改变它的价值
答案 1 :(得分:2)
Globals.BookLL.Single(b => b.BookId == 4).IV = 44;
如果BookIds是唯一的,您可以使用single
。
答案 2 :(得分:0)
Globals.BookLL.Find(B => B.BookID == 4)
这将返回IEnumerable<Globals.Node>
,您应该使用LINQ的First()
:
Globals.BookLL.First(B => B.BookID == 4).Value.IV = 999;