[英]How can I do multiple operations inside a C# LINQ ForEach loop
I have a list of Question objects and I use a ForEach
to iterate through the list. 我有一个Question对象列表,我使用
ForEach
迭代列表。 For each object I do an .Add
to add it into my entity framework and then the database. 对于每个对象,我执行
.Add
将其添加到我的实体框架中,然后添加到数据库中。
List<Question> add = problem.Questions.ToList();
add.ForEach(_obj => _uow.Questions.Add(_obj));
I need to modify each of the objects in the ForEach
and set the AssignedDate
field to DateTime.Now
. 我需要修改
ForEach
中的每个对象,并将AssignedDate
字段设置为DateTime.Now
。 Is there a way I can do this inside of the ForEach
loop? 有没有办法可以在
ForEach
循环中执行此操作?
You would do something like 你会做的事情
add.ForEach(_obj =>
{
_uow.Questions.Add(_obj);
Console.WriteLine("TADA");
});
Have a look at the examples in Action Delegate 看一下Action Delegate中的示例
The following example demonstrates the use of the Action delegate to print the contents of a List object.
以下示例演示如何使用Action委托来打印List对象的内容。 In this example, the Print method is used to display the contents of the list to the console.
在此示例中,Print方法用于向控制台显示列表的内容。 In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console.
此外,C#示例还演示了使用匿名方法向控制台显示内容。 Note that the example does not explicitly declare an Action variable.
请注意,该示例未显式声明Action变量。 Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the List.ForEach method, whose single parameter is an Action delegate.
相反,它传递对一个方法的引用,该方法接受一个参数并且不向List.ForEach方法返回值,该方法的单个参数是Action委托。 Similarly, in the C# example, an Action delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the Action delegate that is expected by the List.ForEach method.
类似地,在C#示例中,未明确实例化Action委托,因为匿名方法的签名与List.ForEach方法所期望的Action委托的签名匹配。
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<String> names = new List<String>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
// Display the contents of the list using the Print method.
names.ForEach(Print);
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach(delegate(String name)
{
Console.WriteLine(name);
});
names.ForEach(name =>
{
Console.WriteLine(name);
});
}
private static void Print(string s)
{
Console.WriteLine(s);
}
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
foreach(var itemToAdd in add)
{
Do_first_thing(itemToAdd);
Do_Second_Thing(itemToAdd);
}
or if you will insist on using the ForEach
method on List<>
或者如果你坚持在
List<>
上使用ForEach
方法
add.ForEach(itemToAdd =>
{
Do_first_thing(itemToAdd);
Do_Second_Thing(itemToAdd);
});
Personally I'd go with the first, it's clearer. 就个人而言,我会选择第一个,它更清晰。
Most likely you don't need to do things this way. 很可能你不需要这样做。 Just use a plain
foreach
: 只需使用简单的
foreach
:
foreach (var question in problem.Questions)
{
question.AssignedDate = DateTime.Now;
_uow.Questions.Add(question);
}
Unless there is specific reason to use a lambda, a foreach
is cleaner and more readable. 除非有特殊原因要使用lambda,否则
foreach
更清晰,更易读。 As an added bonus it does not force you to materialize the collection of questions into a list, most likely reducing your application's memory footprint. 作为额外的奖励,它不会强迫您将问题集合实现到列表中,最有可能减少应用程序的内存占用。
use a foreach statement 使用foreach声明
foreach (Question q in add)
{
_uow.Questions.Add(q);
q.AssignedDate = DateTime.Now;
}
or as astander propose do _obj.AssignedDate = DateTime.Now;
或作为旁观者提议做
_obj.AssignedDate = DateTime.Now;
in the .ForEach(
method 在
.ForEach(
方法
Like this 像这样
add.ForEach(_obj =>
{
_obj.AssignedDate = DateTime.Now;
_uow.Questions.Add(_obj);
});
add.ForEach(delegate(Question q)
{
// This is anonymous method and you can do what ever you want inside it.
// you can access THE object with q
Console.WriteLine(q);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.