40

我有一个 Question 对象列表,我使用 aForEach来遍历列表。对于每个对象,我都会将.Add其添加到我的实体框架中,然后添加到数据库中。

List<Question> add = problem.Questions.ToList();
add.ForEach(_obj => _uow.Questions.Add(_obj));

我需要修改 中的每个对象ForEach并将 AssignedDate字段设置为DateTime.Now. 有没有办法可以在ForEach循环内做到这一点?

4

6 回答 6

76

You would do something like

add.ForEach(_obj =>
                {
                    _uow.Questions.Add(_obj);
                    Console.WriteLine("TADA");
                });

Have a look at the examples in Action Delegate

The following example demonstrates the use of the Action delegate to print the contents of a List object. In this example, the Print method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an Action variable. 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. 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.

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
 */
于 2013-08-14T11:40:07.823 回答
4
foreach(var itemToAdd in add)
{
   Do_first_thing(itemToAdd);
   Do_Second_Thing(itemToAdd);
}

或者如果您坚持使用该ForEach方法List<>

add.ForEach(itemToAdd  => 
{
   Do_first_thing(itemToAdd);
   Do_Second_Thing(itemToAdd);
});

我个人会选择第一个,它更清楚。

于 2013-08-14T11:41:41.160 回答
2

很可能您不需要以这种方式做事。只需使用普通的foreach

foreach (var question in problem.Questions)
{
    question.AssignedDate = DateTime.Now;
    _uow.Questions.Add(question);
}

除非有特定的理由使用 lambda,否则 aforeach更简洁且更具可读性。作为一个额外的好处,它不会强迫您将问题集合具体化为一个列表,这很可能会减少您的应用程序的内存占用。

于 2013-08-14T11:41:47.733 回答
0

使用 foreach 语句

foreach (Question q in add)
{
   _uow.Questions.Add(q);
   q.AssignedDate = DateTime.Now;
}

或如 asstander 建议 _obj.AssignedDate = DateTime.Now;.ForEach(方法中做的那样

于 2013-08-14T11:41:19.633 回答
0

像这样

add.ForEach(_obj =>
                    {
                        _obj.AssignedDate = DateTime.Now;
                        _uow.Questions.Add(_obj);

                    });
于 2013-08-14T11:41:52.770 回答
0
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);
    });
于 2013-08-14T11:45:48.737 回答