Skip to main content
 首页 » 编程设计

c#之从另一个列表对象创建对象列表 LINQ Lambda

2024年02月13日14lvdongjie

我正在尝试将某些对象添加到另一个对象。但是我在“选项”部分遇到错误。我只是简单地尝试将某些东西从一个对象添加到另一个对象中。

这是我的代码的样子..

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

错误是

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

模型:

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

请您参考如下方法:

希望这不会被误导,但我认为您的做法全错了。你想做一个 Select并将结果分配给响应中的问题属性。这是一个基本示例;

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

我很快就编辑了你的代码,所以有一些可能无法按原样工作(没有编译或任何东西)。但基本上你使用 Select对于投影,返回 List<Question>并将其分配给 Questions属性(property)。不要试图就地添加。除此之外,您从未初始化 Questions列出所以即使编译了该代码你也会得到一个NullReferenceException .同样,您的代码可能还有其他问题,但我认为您从根本上滥用了 ForEach什么时候Select实际上是正确的操作。