Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ:进行选择和更新_C#_Linq_Select - Fatal编程技术网

C# LINQ:进行选择和更新

C# LINQ:进行选择和更新,c#,linq,select,C#,Linq,Select,我有这个密码 foreach (MyType o in myList) { var res = from p in myOriginalList where p.PropertyA == o.PropertyA && p.PropertyB == o.PropertyB select p; //Need to update myOriginalList //.... } 我想在myOrigina

我有这个密码

foreach (MyType o in myList)
{
    var res = from p in myOriginalList
              where p.PropertyA == o.PropertyA && p.PropertyB == o.PropertyB
              select p;

    //Need to update myOriginalList

    //....
}
我想在myOriginalList中对Linq select找到的每条记录进行更新。我该怎么做

谢谢

foreach(var item in res)
{
   item.prop = updatedvalue;
}

你是说这个吗?

没有内置的ForEach扩展-所以只需循环和更新:

foreach(var item in res) {
    item.SomeProp = someValue;
}
还请注意,您可以使用
SelectMany
在一个查询中执行此操作:

var res = from MyType o in myList
          from p in myOriginalList
              where p.PropertyA == o.PropertyA && p.PropertyB == o.PropertyB
              select p;

要挑剔的是,有一个ForEach扩展,但不是直接在IEnumerable接口上,而是在泛型列表上(哲学原因:),在第一部分中,您更新的是查询结果而不是“myOriginalList”,对吗?这是基于res结果I的原始更新need@Kris-一,;这取决于你想做什么。通常,当人们说“更新列表”时,他们的意思是“更新列表引用的对象的状态”,即“item.SomeProp=someValue;”。除非您使用的是
struct
s。你的意思是交换列表中的项目吗?i、 e.更改实际参考?