我在一些类似于这个问题的问题中搜索了解决方案,但是无法解决它,所以请提供一个如何解决这个问题的清晰想法.
Order orderDetails= createOrder();
long voucherId = (long)orderDetails.Vouchers.FirstOrDefault().Number; // exception here..
createOrder函数返回Order类型的orderDetails.
优惠券是我的凭证表,
Number是凭证表中的列名称.
我不知道,为什么如何解决这个异常.有什么想法吗?
第一部分:
*编辑:*
private Order createOrder()
{
IList<OfferInfo> offerInformation = new List<OfferInfo>();
OfferInfo offer = new OfferInfo()
{
OfferId = 2,
Message = "test msg",
CreatedDate = System.DateTime.Now,
Gender = "male",
ReceiverName = "john",
ReceiverEmail = "ebenezar@gmail.com"
};
offerInformation.Add(offer);
Order order = new Order();
order.Id = 721;
order.Amount = 1000;
order.CreatedDate = System.DateTime.Now;
order.User = userDetails;
return BLOrder.CreateOrder(order, offerInformation);
}
注意:
它内部从BLOrder调用CreateOrder,它返回Order类型中检索到的数据. (如果var是Order类型并且被返回,它将具有类似于var.xxx =“some value”,var.yyy =“some value”..)
第二部分:
public static Order CreateOrder(Order order, IList<OfferInfo> offerList)
{
order = CreateNewOrder(order, offerList);
Intreat.MSMQ.MSMQHelper.AddOffers(order, offerList);
return order;
}
private static Order CreateNewOrder(Order order, IList<OfferInfo> offerList, bool updateUser = true)
{
try
{
if (updateUser)
{
VerifyUserDetails(order.User);
senderUserId = BLUser.UpdateUser(order.User);
order.User = null;
Logger.WriteLog("Sender user added/updated successfully. UserID:" + senderUserId.ToString());
}
else
senderUserId = order.UserId;
if (order.Company != null)
order.CompanyId = BLCompany.UpdateCompany(order.Company).Id;
VerifyOrderDetails(order, offerList);
using (IntreatEntities intreat = new IntreatEntities())
{
foreach (OfferInfo offer in offerList)
{
orderAmt = (double)((from po in intreat.PartnerOffers
where po.Id == offer.OfferId
select po.Price * offer.Quantity).ToList()).Sum();
order.Amount += orderAmt;
if (offer.IsPos)
tableOrderAmt += orderAmt;
}
if ((tableOrderAmt > 0) && (order.TipPercentage != null) && (order.TipPercentage.Value) > 0)
{
double tipAmt = (tableOrderAmt * (order.TipPercentage.Value * .01));
order.TipAmount = Math.Round(tipAmt);
order.Amount = (double)(order.Amount + order.TipAmount);
}
order.CreatedDate = DateTime.Now;
order.UserId = (Guid)senderUserId;
Logger.WriteLog(string.Format(CultureInfo.InvariantCulture, "order.TableNumber:{0}", order.TableNumber == null ? "(empty)" : order.TableNumber.ToString()));
intreat.Orders.AddObject(order);
intreat.SaveChanges();
Logger.WriteLog("Order added successfully. OrderId:" + order.Id.ToString(CultureInfo.InvariantCulture));
}
return order;
}
catch (Exception ex)
{
Logger.WriteLog(ex);
throw;
}
}
最佳答案 合并这行代码order.Vouchers.Load();如下所示并尝试
private static Order CreateNewOrder(Order order, IList<OfferInfo> offerList, bool updateUser = true)
{
try
{
Guid? senderUserId = null;
/// Process user first so that, the user details are stored even if there is any error in other areas
///
if (updateUser)
{
VerifyUserDetails(order.User);
/// Create/update sending user.
///
senderUserId = BLUser.UpdateUser(order.User);
order.User = null;
Logger.WriteLog("Sender user added/updated successfully. UserID:" + senderUserId.ToString());
}
else
senderUserId = order.UserId;
/// Add company details before processing the order, so that the company details are stored if there is any error in other areas.
///
if (order.Company != null)
order.CompanyId = BLCompany.UpdateCompany(order.Company).Id;
/// Verify and process order
///
VerifyOrderDetails(order, offerList);
using (IntreatEntities intreat = new IntreatEntities())
{
/// Find total amount for the order
double orderAmt = 0;
double tableOrderAmt = 0;
order.Amount = 0;
foreach (OfferInfo offer in offerList)
{
orderAmt = (double)((from po in intreat.PartnerOffers
where po.Id == offer.OfferId
select po.Price * offer.Quantity).ToList()).Sum();
order.Amount += orderAmt;
//If isPos, consider for tip calculation
if (offer.IsPos)
tableOrderAmt += orderAmt;
}
//check if tip amount has to be calculated, by checking for the tableorderAmt
if ((tableOrderAmt > 0) && (order.TipPercentage != null) && (order.TipPercentage.Value) > 0)
{
double tipAmt = (tableOrderAmt * (order.TipPercentage.Value * .01));
order.TipAmount = Math.Round(tipAmt);
order.Amount = (double)(order.Amount + order.TipAmount);
}
order.CreatedDate = DateTime.Now;
order.UserId = (Guid)senderUserId;
Logger.WriteLog(string.Format(CultureInfo.InvariantCulture, "order.TableNumber:{0}", order.TableNumber == null ? "(empty)" : order.TableNumber.ToString()));
/// Create and save order in db
///
intreat.Orders.AddObject(order);
intreat.SaveChanges();
order.Vouchers.Load();
Logger.WriteLog("Order added successfully. OrderId:" + order.Id.ToString(CultureInfo.InvariantCulture));
}
return order;
}
catch (Exception ex)
{
Logger.WriteLog(ex);
throw;
}
}