是否可以在内联对象声明中使用循环?

时间:2014-07-18 07:03:13

标签: c# asp.net

ListOfComment是对象的List<Comment>属性,最好的方法是:

                        ListOfComment = new List<Comment>
                        {
                            foreach(object a in b)
                            {
                                new Comment
                                {
                                    Type = "",
                                    Description = ""
                                }
                            }
                        }

1 个答案:

答案 0 :(得分:0)

这在对象声明中是不可能的,你可以这样做

var comments = (from a in b
                select new Comment
                {
                     Type = "",
                     Description = "";
                }).ToList();

您可以做的是(具有固定数量的条目)

var comments = new List<Comment>()
{
    new Comment { Type= "", Description = "" },
    new Comment { Type= "", Description = "" },
};