0

我有一个简单的方法:

    private IList<Objective> createObjectives()
    {

        var objectiveNames = new[]
        {
            "Objective 1",
            "Objective 2",
            "Objective 3",
            "Objective 4",
            "Objective 5",
            "Objective 6",
            "Objective 7",
            "Objective 8"
        };
        var objectives = from objectiveName in objectiveNames
                         select new Objective
                         {
                             Name = objectiveName,
                             Description = objectiveName + " Description",
                             ModifiedDate = DateTime.Now
                         };
        return objectives.ToList();
    }

这是我可以转换为使用 lambdas 的东西吗?这样做有什么好处吗?

4

1 回答 1

1
objectiveNames.Select(o => new Objective
                         {
                             Name = o,
                             Description = o + " Description",
                             ModifiedDate = DateTime.Now
                         }).ToList();

很难说有什么优势,但我更喜欢 Lambdas,因为我不想在程序中出现类似 sql 的编码。

于 2013-03-27T01:20:21.143 回答