我正在尝试为具有特定订单的特定客户输出付款计划。
我不断收到此错误:
info:Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'null'.
我做错了什么,但不确定是什么。寻找解决方案很长时间没有运气。我是编程和C#的新手,所以仍然想摆脱这个问题。非常感谢您的帮助和指导。
这是我到目前为止所拥有的:
型号:
using System.Collections.Generic;
namespace Project.Models
{
public class Customer
{
public string CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DateOfBirth { get; set; }
}
public class Order
{
public string OrderId { get; set; }
public string Product { get; set; }
public string Status { get; set; }
public double Price { get; set; }
}
public class CustomerOrder
{
public string CustomerId {get; set;}
public string OrderId { get; set; }
}
public class Repayment
{
public int Amount { get; set; }
public string DueDate { get; set; }
public string RepaymentId { get; set; }
}
public class Choice
{
public string ChoiceId { get; set; }
public List<Repayment> Repayments{ get; set; }
}
public class RepaymentPlan
{
public List<Choice> Choices{ get; set; }
}
}
存储库:
using System.Collections.Generic;
using System.Linq;
using Project.Models;
using System.Net;
using System.Collections.Specialized;
using System.Text;
namespace Project.Repositories
{
public class OrderRepository
{
private static List<Order> _Orders;
private static List<CustomerOrder> _CustomerOrder;
private static List<RepaymentPlan> _RepaymentPlan;
static OrderRepository()
{
_Orders = new List<Order>();
_CustomerOrder= new List<CustomerOrder>();
_PaymentPlan = new List<PaymentPlan>();
_Orders.Add(new Order
{
OrderId = "124",
Product= "Shirts",
Status= "On it's way",
Price= 100.20,
});
_Orders.Add(new Order
{
OrderId= "122",
Product= "Pants",
Status= "Not ready",
Price= 300.30,
});
_Orders.Add(new Order
{
OrderId= "143",
Product= "Deadpool",
Status= "On it's way",
Price= 6.20,
});
_Orders.Add(new Order
{
OrderId= "156",
Product= "Socks",
Status= "Not ready",
Price= 3.30,
});
_CustomerOrder.Add(new CustomerOrder
{
CustomerId = "578",
OrderId = "156",
});
_RepaymentPlan.Add(new RepaymentPlan
{
choices = new List<Choice>
{
new Choice
{
choiceId = "cho1",
Repayments = new List<Repayment>
{
new Repayment
{
amount = 200,
dueDate = "2018-06-01"
},
new Repayment
{
amount = 100,
dueDate = "2018-08-01",
}
}
},
new Choice
{
choiceId = "cho2",
repayments = new List<Repayment>
{
new Repayment
{
repaymentId = "Choice1",
amount = 300,
dueDate = "2018-10-01"
},
new Repayment
{
repaymentId = "Choice2",
amount = 150,
dueDate = "2018-11-01"
},
}
}
},
});
}
public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)
{
var customerlist =_CustomerOrder.FindAll(c => c.CustomerId==customerId);
var orderlist= _CustomerOrder.FindAll(c => c.orderId==orderId);
var pplist= new RepaymentPlan();
if (customerlist == orderlist)
{
return pplist;
}
return null;
}
控制器:
OrderController.cs:
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Project.Models;
using Project.Repositories;
namespace Project.Controllers
{
public class OrderController : Controller
{
[HttpGet("customers/{customerid}/orders/{orderId}/paymentPlan")]
public RepaymentPlan FindRepaymentPlan(string customerId, string orderId)
{
return OrderRepository.GetRepaymentPlan(customerId, orderId);
}
}
}
答案 0 :(得分:2)
好的,仔细检查代码后,我发现问题出在此方法中
public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)
您将始终返回null
,因为您的情况将始终为false
。这适用于任何(可能)编程语言。
if (customerlist == orderlist)
评估两个对象是否相同(它们引用相同的内存地址),而不评估这些对象的内容。要查看两个对象是否具有相同的数据,可以使用循环(for
,while
)。但是,由于您使用了Linq,因此在FindAll
函数中使用多个条件就足够了。
public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)
{
var customerlist =_CustomerOrder.FindAll(c => c.CustomerId==customerId && c.orderId==orderId);
var pplist= new RepaymentPlan();
if (customerlist.Any())
{
return pplist;
}
return null;
}
此外,您可能希望返回customerlist.First()
而不是pplist
。如果返回类型为RepaymentPlan
的空对象,则可能对您没有帮助。