3

I have the following class:

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; }

}

and the following code:

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

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

How can I modify this so I can specify both the "Title" and some small sample "Text" in the myData array. I was thinking about using an array of objects but I am not quite sure how to set this up.

1
  • Lambdas are very powerful - they can take multiple parameters, they can reference and modify external variables, etc. Try some things you think might work and you may be surprised. Commented Jun 14, 2013 at 4:05

2 Answers 2

1

Here is the syntax for that:

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}
            };
Sign up to request clarification or add additional context in comments.

1 Comment

Can you show me how I would fill the data into contents using the lambda. I am not sure how to do that with it in an object now.
1

How about you use Tuple?

        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
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.