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

标签 c# asp.net-mvc linq list lambda

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

这是我的代码的样子..

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实际上是正确的操作。

关于c# - 从另一个列表对象创建对象列表 LINQ Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022867/

相关文章:

c# - 在 MVVM 应用程序中切换 ViewModel 时出现 BindingExpression 路径错误

c# - 如何远程启动不同系统上的应用程序?

c# - SignalR.EventAggregatorProxy 简单示例问题

c# - MSMQ - 无法从多播队列接收

javascript - 通过电线将数据从 js 发送到 Controller

c# - Controller 名称的 MVC 路由约束

c# - 可空 bool 列的 LINQ Where 子句中的三元运算符

c# - 为什么controllers action有HttpRequestBase,而viewpage有HttpRequest?

c# - 将函数或存储过程结果转换为 LINQ 的 "live"结果

c# - 使用 Entity Framework 添加多个对象时如何获取多个对象的标识?