allencoded
allencoded

Reputation: 7275

Creating an Object List from another List Object LINQ Lambda

I am trying to add certain objects to another object. But I am getting an error in the Options section. I am quite simply trying to add certain stuff out of one object and into another.

Here is what my code looks like..

var responses = new Responses();
                form.Questions.ForEach(
                    q => responses.Questions.Add(new Models.Question()
                    {
                        QuestionId = Convert.ToInt32(q.Id),
                        Value = q.SingleAnswer,
                        Options = q.Options.ForEach( o => q.Options.Add(
                            new Option // <----FAILING HERE!!!!!!!!!!!!
                            {
                                OptionId = 1,
                                Value = "test"
                            }
                            ))

                    })
                );

The error is

Argument type 'Web.Models.Option' is not assignable to parameter type QuestionOptionViewModel

MODELS:

public class Responses
    {
        public List<Question> Questions { get; set; } 
    }

    public class Question
    {
        public int QuestionId { get; set; }
        public string Value { get; set; }
        public List<Option> Options { get; set; }
    }

    public class Option
    {
        public int OptionId { get; set; }
        public string Value { get; set; }
    }


public class QuestionOptionViewModel
    {
        public int? Id { get; set; }

        public string Text { get; set; }

        public string QuestionType { get; set; }

        [RequiredIf("QuestionType", "text", ErrorMessage = "Required Field")]
        public string Value { get; set; }

        [RequiredIf("QuestionType", "checkbox", ErrorMessage = "Required Field")]
        public bool IsChecked { get; set; }
    }

public class QuestionViewModel
    {
        public int? Id { get; set; }

        public string QuestionType { get; set; }

        public string SubType { get; set; }

        public string Text { get; set; }

        public int SortOrder { get; set; }

        public bool IsHidden { get; set; }

        [RequiredIf("QuestionType", "singleAnswer", ErrorMessage = "Reqired Field")]
        public string SingleAnswer { get; set; }

        [RequiredIf("QuestionType", "radio", ErrorMessage = "Radio Reqired")]
        public int? SelectedRadio { get; set; }

        [RequiredIf("QuestionType", "select", ErrorMessage = "Selection Reqired")]
        public int? SelectedSelect { get; set; }

        public bool CheckboxError { get; set; }

        public List<QuestionOptionViewModel> Options { get; set; }
    }

Upvotes: 1

Views: 8915

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48114

Hopefully this isn't too misguided but I think you're going about this all wrong. You want to do a Select and assign the result to the questions property in responses. Here's a basic example;

var responses = new Responses();
    responses.Questions = form.Questions.Select(
                q => new Models.Question()
                {
                    QuestionId = Convert.ToInt32(q.Id),
                    Value = q.SingleAnswer,
                    Options = q.Options.Select(o =>
                        new Option
                        {
                            OptionId = (int) o.Id,
                            Value = o.Value
                        }).ToList()
                }).ToList();

I edited your code rather quickly so there is some potential that won't work as is (didn't compile or anything). But basically you use Select for projection, return a List<Question> and assign it to the Questions property. Don't try to do the adds in place. Besides that you never initialized the Questions list so even if that code compiled you'd get a NullReferenceException. Again, there are likely other problems with your code but I think you're fundamentally misusing ForEach when Select is actually the correct operation.

Upvotes: 3

juharr
juharr

Reputation: 32296

There are two issues here. One you are trying to change the collection you are iterating over with your ForEach. And second you are trying to assign the result of that ForEach. Instead you should use a Select and ToList to create a list to assign to Options. If you change

Options = q.Options.ForEach( o => q.Options.Add(
      new Option 
      {
            OptionId = 1,
            Value = "test"
       }
       ))

to

Options = q.Options.Select( 
      new Option 
      {
            OptionId = 1,
            Value = "test"
       }
       ).ToList()

it should work

Upvotes: 1

Related Questions