4

我在一些类似于此问题的问题中搜索了解决方案,但无法解决,因此请提供有关如何解决此问题的清晰想法。

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;
        }
    }
4

1 回答 1

2

合并这行代码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;
    }
}
于 2013-03-21T09:12:53.947 回答