[英]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; }
}
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. 您想要执行
Select
并将结果分配给响应中的问题属性。 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. 但基本上,您使用
Select
进行投影,返回List<Question>
并将其分配给Questions
属性。 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
. 除此之外,您永远不会初始化
Questions
列表,因此即使编译了该代码,您也会得到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. 同样,您的代码可能还会存在其他问题,但是当
Select
实际上是正确的操作时,我认为您从根本上滥用了ForEach
。
There are two issues here. 这里有两个问题。 One you are trying to change the collection you are iterating over with your
ForEach
. 您正在尝试更改要用
ForEach
迭代的集合。 And second you are trying to assign the result of that ForEach
. 其次,您正在尝试分配该
ForEach
的结果。 Instead you should use a Select
and ToList
to create a list to assign to Options
. 相反,您应该使用
Select
and ToList
创建一个列表以分配给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 它应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.