我有一个简单的方法:
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的东西吗?我这样做有什么好处吗?
答案 0 :(得分:1)
objectiveNames.Select(o => new Objective
{
Name = o,
Description = o + " Description",
ModifiedDate = DateTime.Now
}).ToList();
很难说有什么优势,但我更喜欢Lambdas,因为我不想在程序中使用类似sql的编码。