简体   繁体   English

LINQ C#MVC4空引用异常

[英]LINQ c# mvc4 null reference exception

I am trying to learn LINQ and I am having a problem with a null ref exception error that I can't understand. 我正在尝试学习LINQ,但遇到无法理解的null ref异常错误的问题。

Overview of my project/intent: I have 2 tables in the DB. 项目/意图概述:数据库中有2个表。 One is Customers and one is Orders. 一个是客户,一个是订单。 The Customers table has the ID, Name columns. 客户表具有ID,名称列。 The Orders table has orderID, Description and CustomerID column. Orders表具有orderID,Description和CustomerID列。

Customers
ID       Name
1        eeee
2        dddd
3        ffff

Orders
ID       Description   CustomerID
1        sdffds        2
2        dsfasdf       2
3        fjdsa         1
4        dfsa          3

What I am trying to do is to select and display only the customers that have more then 1 orders (in this case, only customer with ID 2). 我想做的是仅选择并显示订单数量超过1的客户(在这种情况下,仅显示ID为2的客户)。

I have a ViewModel class 我有一个ViewModel类

 public class ViewModel
    {
      public List<Customer> Customers { get; set; } //list of customer objects 
      public List<Order> Orders { get; set; } //list of customer objects 
    }

In my homecontroller's index action, I have: 在我的主控制器的索引操作中,我有:

        public ActionResult Index()
        {

         Context db = new Context(); //create new Context Object named db which contains lists of objects pulled from database server

         ViewModel model = new ViewModel(); //create new ViewModel Object named model which contains lists of objects

         model.Customers = db.Customers.ToList(); //pull Customers data table from db 

         var groups = db.Orders.GroupBy(custgroup => custgroup.CustomerId).Where(group => group.Count()>1); //pull Orders data table from db and group by CustomerID, and choose only groups with more than 1 records.

            foreach (var group in groups)
            {
                foreach (Order order in group)
                //foreach (var order in group)
                {
                    model.Orders.Add(order); //***The null exception occurs here where 'Orders' is null. Check for null before calling method***
                }
            }


            return View(model);
           }

In effect, what I am trying to do is to group orders by customers and select the groups of orders of my choosing. 实际上,我想做的是按客户对订单进行分组,然后选择自己选择的订单组。 Then put back the individual records from the groups into the original object format. 然后将各组中的各个记录放回原始对象格式。 From the debug, I think I have achieved the filtering process. 通过调试,我认为我已经实现了过滤过程。 The problem occurs when I try to put the records back into the 'model.Orders' list. 当我尝试将记录放回到“ model.Orders”列表中时,会发生问题。

The null exception occurs inside the inner foreach where 'Orders' list is null. 空异常发生在内部foreach内部,其中“订单”列表为空。 The error indication is that the .Orders list is null and/or has not been declared. 错误指示是.Orders列表为null和/或尚未声明。 But I am thinking that the list was declared in the statement ViewModel model = new ViewModel(); 但我认为该列表已在语句ViewModel model = new ViewModel();声明ViewModel model = new ViewModel(); at the top. 在顶部。

How do I fix this null exception error? 如何解决此null异常错误? TIA. TIA。

You forgot to initialize model.Orders. 您忘记初始化model.Orders。

Put

model.Orders = new List<Order>();

after you create the model and it should work. 创建模型后,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM