在方法内分配Icollection

时间:2018-06-06 06:20:37

标签: asp.net-mvc

如何将getPur()方法正确分配给Transaction.PurchasedItems 我需要一个结果集,它是TransactionPurchasedItems

的组合

我正在寻找与图像相同的结果。 现在,我正在制作一个包含所有列的新模型,从而获得理想的结果。但是如果没有制作新模型,这是否可能呢?

enter image description here

我尝试过的是 但我在这一行收到错误

VM.Transaction.PurchasedItems = getPur().ToList().Where(t => t.CustomerId == id).ToList();

  

对象引用未设置为对象的实例。

请指导我制作这个契约。

型号:

    public class Transaction
    {
        public int TransactionId { get; set; }

        public string Progress { get; set; }
        public int ProgressId { get; set; }

        public string Status { get; set; }
        public int StatusId { get; set; }

        public decimal Net { get; set; }

        public DateTime Date_ { get; set; }

        public virtual ICollection<PurchasedItem> PurchasedItems { get; set; }
    }
    public class PurchasedItem
    {
        public int CustomerId { get; set; }
        string Product { get; set; }
        decimal Total { get; set; }
        public string ProductRemarks { get; set; }
    }

控制器

public ActionResult History(int? id)
        {
            CustomerViewModel VM = new CustomerViewModel();
            VM.Transactions = getTransactions().Where(t => t.CustomerId == id);
            VM.Transaction.PurchasedItems = getPur().Where(t => t.CustomerId == id).ToList();

            return View(VM);
        }

public IEnumerable<PurchasedItem> getPur()
        {
            var prod = (from c in db.Customers.Include(c => c.CUSTOMERTYPE)
                        join ch in db.CUSTOMERHEADs.Include(ch => ch.CustomerDatas).Include(ch => ch.Customer).Include(ch => ch.CustomerProgress).Include(ch => ch.CustomerStatu) on c.Id equals ch.CustomerId into l2
                        from ch in l2.DefaultIfEmpty()
                        join sm in db.SalesMen on ch.SalesManId equals sm.MasterId into l1
                        from sm in l1.DefaultIfEmpty()
                        join cd in db.CustomerDatas on ch.id equals cd.Headid into l3
                        from cd in l3.DefaultIfEmpty()
                        select new
                        {
                            cid = c.Id,
                            prod = cd.ProductCode,
                            price = cd.Price,
                            qty = cd.Qty,
                            disc = cd.Disc,
                            amount = cd.Amount,
                            remarks = cd.Remarks,
                            progressid = ch.CustomerProgress == null ? 0 : ch.CustomerProgress.Id,

                        });
            var details = new List<PurchasedItem>();
            foreach (var t in prod)
            {
                details.Add(new PurchasedItem()
                {
                    CustomerId = t.cid,
                    ProgressId = t.progressid,
                    Product = t.prod,
                    Qty = Convert.ToDecimal(t.qty),
                    Price = Convert.ToDecimal(t.price),
                    Disc = Convert.ToDecimal(t.disc),
                    Total = Convert.ToDecimal(t.amount),
                    ProductRemarks = t.remarks,

                });
            }
            return details;
        }

更新

我所做出的更正

public ActionResult History(int? id)
        {
            var details = getTransactions().Where(t => t.CustomerId == id);
            return View(details);
        }

public IEnumerable<Transaction> getTransactions()
        {
            var cust = (from c in db.Customers.Include(c => c.CUSTOMERTYPE)
                        join ch in db.CUSTOMERHEADs.Include(ch => ch.Customer).Include(ch => ch.CustomerProgress).Include(ch => ch.CustomerStatu)
                        .Include(ch => ch.CustomerDatas) on c.Id equals ch.CustomerId into l2
                        from ch in l2.DefaultIfEmpty()
                        join sm in db.SalesMen on ch.SalesManId equals sm.MasterId into l1
                        from sm in l1.DefaultIfEmpty()
                        select new
                        {
                            hid = ch==null?0:ch.id,
                            customerid = c.Id,
                            voucherno = ch.VoucherNo,
                            progress = ch.CustomerProgress.Status,
                            date = ch.Date_==null?DateTime.MinValue : ch.Date_,
                            appointmentDate = ch.Date2_,
                            status = ch.CustomerStatu.Status,
                            sales = sm.Name,
                            notes = ch.Notes,
                            progressid = ch.CustomerProgress == null?0 :ch.CustomerProgress.Id,
                            cd = ch.CustomerDatas.ToList(),

                        });

            var details = new List<Transaction>();

            foreach (var t in cust)
            {
                details.Add(new Transaction()
                {
                    CustomerId = t.customerid,
                    voucherno = t.voucherno,
                    Progress = t.progress,
                    ProgressId = t.progressid,
                    Date_ = t.date,
                    Date2_ = t.appointmentDate,
                    Status = t.status,
                    Salesman = t.sales,
                    AppointmentRemarks = t.notes,
                    PurchasedItems = getPurchase().Where(m => m.Headid == t.hid).ToList(),

                });
            }
            return details;
        }

        public IEnumerable<PurchasedItem> getPurchase()
        {
            var prod = (from c in db.Customers.Include(c => c.CUSTOMERTYPE)
                        join ch in db.CUSTOMERHEADs.Include(ch => ch.CustomerDatas).Include(ch => ch.Customer).Include(ch => ch.CustomerProgress).Include(ch => ch.CustomerStatu) on c.Id equals ch.CustomerId into l2
                        from ch in l2.DefaultIfEmpty()
                        join sm in db.SalesMen on ch.SalesManId equals sm.MasterId into l1
                        from sm in l1.DefaultIfEmpty()
                        join cd in db.CustomerDatas on ch.id equals cd.Headid into l3
                        from cd in l3.DefaultIfEmpty()
                        select new
                        {
                            cid = c.Id,
                            prod = cd.ProductCode,
                            price = cd.Price,
                            qty = cd.Qty,
                            disc = cd.Disc,
                            amount = cd.Amount,
                            remarks = cd.Remarks,
                            progressid = ch.CustomerProgress == null ? 0 : ch.CustomerProgress.Id,
                            hid = cd==null?0:cd.Headid,

                        });
            var details = new List<PurchasedItem>();
            try
            {
                foreach (var t in prod)
                {
                    details.Add(new PurchasedItem()
                    {
                        Headid = Convert.ToInt32(t.hid),
                        CustomerId = t.cid,
                        ProgressId = t.progressid,
                        Product = t.prod,
                        Qty = Convert.ToDecimal(t.qty),
                        Price = Convert.ToDecimal(t.price),
                        Disc = Convert.ToDecimal(t.disc),
                        Total = Convert.ToDecimal(t.amount),
                        ProductRemarks = t.remarks,

                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return details;
        }

1 个答案:

答案 0 :(得分:2)

您似乎忘了初始化 club 本身。

修复很简单:

VM.Transaction

您也可以选择通过创建VM.Transaction = new Transaction(); // <--- or whatever is needed / suitable VM.Transaction.PurchasedItems = ..... 构造函数并在该构造函数中设置CustomerViewModel来执行此操作,因此每次创建Transaction实例时,它都会初始化{ {1}}属性。