3

我有以下课程:

public partial class Content
{

    public int ContentId { get; set; }
    public int ContentTypeId { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public System.DateTime ModifiedDate { get; set; }
    public virtual byte[] Version { get; set; }

    public int SubjectId { get; set; }
    public virtual Subject Subject { get; set; }

}

和以下代码:

var myData = new[] {
   "Content 1",
   "Content 2",
   "Content 3" 
};

var contents = myData.Select(e => new Content
{
   Title = e,
   Text = "xx",
   ModifiedDate = DateTime.Now
}

我该如何修改它,以便我可以在 myData 数组中指定“标题”和一些小样本“文本”。我正在考虑使用一组对象,但我不太确定如何设置它。

4

2 回答 2

1

这是它的语法:

var myData = new List<Content>
            {
              new Content{Title = "Content 1", Text = "xx", ModifiedDate = DateTime.Now},
              new Content{Title = "Content 2", Text = "AB", ModifiedDate = DateTime.Now},
              new Content{Title = "Content 3", Text = "CC", ModifiedDate = DateTime.Now}
            };
于 2013-06-14T04:07:22.720 回答
1

你如何使用元组?

        var myDatas = new[]
        {
            new Tuple<string, string, DateTime>("Title", "Example", DateTime.Now),
            new Tuple<string, string, DateTime>("Title2", "Example", DateTime.Now.AddDays(-1)),
            new Tuple<string, string, DateTime>("Title3", "Example", DateTime.Now.AddDays(1))
        };

        var contents = myDatas.Select(e => new Content
        {
            Title = e.Item1,
            Text = e.Item2,
            ModifiedDate = DateTime.Now
        });
于 2013-06-14T04:17:59.717 回答