Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用linq查询ASP.NET MVC 5实体框架填充视图模型_C#_Asp.net_Asp.net Mvc_Linq_Entity Framework - Fatal编程技术网

C# 如何使用linq查询ASP.NET MVC 5实体框架填充视图模型

C# 如何使用linq查询ASP.NET MVC 5实体框架填充视图模型,c#,asp.net,asp.net-mvc,linq,entity-framework,C#,Asp.net,Asp.net Mvc,Linq,Entity Framework,好的,这是我的数据库结构的后续,我的数据库结构包括一个调查,有很多类别,一个类别有很多问题,然后一个问题有很多问题结果,最后一个问题结果有很多类别结果,在HomeController中,我通过ViewModel绑定模型,但是我现在要求它通过Linq的上下文填充我的实体,非常感谢您的帮助。 var survey = from s in db.Surveys from c in db.Categories

好的,这是我的数据库结构的后续,我的数据库结构包括一个调查,有很多类别,一个类别有很多问题,然后一个问题有很多问题结果,最后一个问题结果有很多类别结果,在HomeController中,我通过ViewModel绑定模型,但是我现在要求它通过Linq的上下文填充我的实体,非常感谢您的帮助。

var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
我需要用我的实体填充这个ViewModel,它是在一个非常有价值的堆栈溢出贡献者的帮助下创建的,所有代码如下所示:

var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
主要问题:如何将实体引入SurveyVM ViewModel实例?

var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
视图模型

namespace MyApp
{
    public class SurveyVM
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<CategoryVM> Categories { get; set; }
    }

    public class CategoryVM
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<QuestionVM> Questions { get; set; }
    }

    public class QuestionVM
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public int Score { get; set; }
    }

    public class QuestionResult
    {
        public int ID { get; set; }
        public int QuestionId { get; set; }
        public int QuestionScore { get; set; }
    }
}
var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
名称空间MyApp
{
公共类调查
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共列表类别{get;set;}
}
公共类类别
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共列表问题{get;set;}
}
公开课问题
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共整数分数{get;set;}
}
公开课提问结果
{
公共int ID{get;set;}
public int QuestionId{get;set;}
公共int问题分数{get;set;}
}
}
链接到实体和后续数据库的模型

public class Question
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public virtual Category Category { get; set; }
        public int CategoryId { get; set; }
        public virtual ICollection<QuestionResult> QuestionResult { get; set; }
        public virtual ICollection<QuestionFeedback> QuestionFeedback { get; set; }
    }
public class QuestionResult
    {
        public int Id { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public int QuestionScore { get; set; }
        //navigation properties
        public virtual ApplicationUser User { get; set; }
        public ICollection<CategoryResult> CategoryResult { get; set; }
        public virtual Question Question { get; set; }
        public int QuestionId { get; set; }
    }
public class CategoryResult
    {
        public int Id { get; set; }
        public int CategoryScore {get;set;}
        //navigation properties
        public virtual QuestionResult QuestionResult { get; set; }
        public int QuestionResultId { get; set; }

    }
public class Category
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public virtual Survey Survey { get; set; }
        public int SurveyId { get; set; }
        public virtual ICollection<Question> Question { get; set; }
        public virtual ICollection<CategoryFeedback> CategoryFeedback { get; set; }
    }
var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
公开课问题
{
公共int Id{get;set;}
公共字符串标题{get;set;}
通过{get;set;}创建的公共字符串
公共日期时间?DateCreated{get;set;}
公共日期时间?日期修改{get;set;}
公共虚拟类别{get;set;}
public int CategoryId{get;set;}
公共虚拟ICollection QuestionResult{get;set;}
公共虚拟ICollection问题反馈{get;set;}
}
公开课提问结果
{
公共int Id{get;set;}
公共日期时间?DateCreated{get;set;}
公共日期时间?日期修改{get;set;}
公共int问题分数{get;set;}
//导航属性
公共虚拟应用程序用户{get;set;}
公共ICollection类别Result{get;set;}
公共虚拟问题{get;set;}
public int QuestionId{get;set;}
}
公共类CategoryResult
{
公共int Id{get;set;}
公共int分类核心{get;set;}
//导航属性
公共虚拟问题结果问题结果{get;set;}
public int QuestionResultId{get;set;}
}
公共类类别
{
公共int Id{get;set;}
公共字符串标题{get;set;}
通过{get;set;}创建的公共字符串
公共日期时间?DateCreated{get;set;}
公共日期时间?日期修改{get;set;}
公共虚拟调查{get;set;}
public int SurveyId{get;set;}
公共虚拟ICollection问题{get;set;}
公共虚拟ICollection类别反馈{get;set;}
}
HomeController

public ActionResult IndexSurveyViewModel()
        {
            SurveyVM model = new SurveyVM()
            {
                ID = 1,
                Name = "Survey 1",
                Categories = new List<CategoryVM>()
                {
                    new CategoryVM()
                    {
                        ID = 1, 
                        Name = "Category A", 
                        Questions = new List<QuestionVM>()
                        {
                            new QuestionVM()
                            {
                                ID = 1, Title = "Question 1A", Score = 2
                            }, 
                                new QuestionVM()
                            {
                                ID = 2, Title = "Question 2A", Score = 4
                            }
                        }
                    },
                    new CategoryVM()
                    {
                        ID = 1, 
                        Name = "Category B", 
                        Questions = new List<QuestionVM>()
                        {
                            new QuestionVM()
                            {
                                ID = 3, Title = "Question 1B", Score = 3
                            }, 
                                new QuestionVM()
                            {
                                ID = 4, Title = "Question 2B", Score = 5
                            }
                        }
                    },
                    new CategoryVM()
                    {
                        ID = 1, 
                        Name = "Category C", 
                        Questions = new List<QuestionVM>()
                        {
                            new QuestionVM()
                            {
                                ID = 5, Title = "Question 1C", Score = 1
                            }, 
                                new QuestionVM()
                            {
                                ID = 6, Title = "Question 2C", Score = 3
                            }
                        }
                    }   
                }
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult IndexSurveyViewModel(SurveyVM model)
        {
            List<QuestionResult> results = new List<QuestionResult>();
            foreach (CategoryVM category in model.Categories)
            {
                foreach (QuestionVM question in category.Questions)
                {
                    results.Add(new QuestionResult()
                    {
                        Id = question.ID,
                        QuestionScore = question.Score
                    });

                }
            }
            return View(model);
        }
var survey = from s in db.Surveys
                         from c in db.Categories
                         from q in db.Questions
                         where s.Id == c.SurveyId
                         where c.Id == q.CategoryId
                         select new SurveyVM
                         {
                             ID = s.Id,
                             Name = s.Title,
                             Categories = new List<CategoryVM>()
                             {
                                 new CategoryVM()
                                 {
                                     ID = c.Id,
                                     Name = c.Title,
                                     Questions = new List<QuestionVM>()
                                     {
                                         new QuestionVM()
                                         {
                                             ID = q.Id,
                                             Title = q.Title
                                         }
                                     }
                                 }
                             }
                         };
public ActionResult IndexSurveyViewModel()
{
SurveyVM模型=新SurveyVM()
{
ID=1,
Name=“调查1”,
类别=新列表()
{
新类别
{
ID=1,
Name=“A类”,
问题=新列表()
{
新问题vm()
{
ID=1,Title=“问题1A”,得分=2
}, 
新问题vm()
{
ID=2,Title=“问题2A”,分数=4
}
}
},
新类别
{
ID=1,
Name=“B类”,
问题=新列表()
{
新问题vm()
{
ID=3,Title=“问题1B”,得分=3
}, 
新问题vm()
{
ID=4,Title=“问题2B”,分数=5
}
}
},
新类别
{
ID=1,
Name=“C类”,
问题=新列表()
{
新问题vm()
{
ID=5,Title=“问题1C”,分数=1
}, 
新问题vm()
{
ID=6,Title=“问题2C”,分数=3
}
}
}   
}
};
返回视图(模型);
}
[HttpPost]
公共行动结果索引SurveyView模型(SurveyVM模型)
{
列表结果=新列表();
foreach(model.Categories中的CategoryVM类别)
{
foreach(问题类别中的问题)
{
结果。添加(新问题结果()
{
Id=question.Id,
QuestionScore=question.Scor