0

我想要做的是创建一个使用 WCF 服务的方法来通过一个数据库(事务)从具有今天日期的表中获取所有内容,然后将它们添加到我的每日销售表中,每个日期将有一行显示利润、日常收入、费用等。

我试过这样做

        public void CalculateProfit(string Date)
        {

            decimal takings = 0;// not needed
            decimal Expenses = 0;// not needed
            using (transactionClassDataContext cont = new transactionClassDataContext())
            { 
                int counter = 0;
                DailySale d = new DailySale();
                var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);
                var query2 = (from r in cont.Transactions where r.Date.Equals(Date) select r);
                foreach (var z in query)
                {
                    counter++;

                }
                if (counter>0)
                {

                        foreach (var y in query2)
                        {

                            takings = takings + y.Price;
                            Expenses = Expenses + 0;

                            d.Expenses += 0;
                            d.Takings += y.Price;
                            d.Profit = d.Takings - d.Expenses;
                            d.Date = Date;

                            cont.DailySales.InsertOnSubmit(d);// update the value
                            cont.SubmitChanges();

                        }              
                }
                else
                {

                    d.Date = Date;
                    cont.DailySales.InsertOnSubmit(d);// if there isnt an entry for todays date, add one
                    cont.SubmitChanges();
                }

            }

        }


    }
}

但它所做的只是抛出这个错误“无法添加已经存在的实体”。

大多数类似的问题都说我需要在 foreach 中创建一个新的 d 实例,但似乎所做的只是将大量记录添加到 m 每日销售额中,而我想要的只是一行更新总数。

有任何想法吗?

4

4 回答 4

1

您应该将其移动DailySale d = new DailySale();到使用它的范围内。

于 2013-04-05T11:08:20.503 回答
0

您正在为 foreach 语句中的每个回合添加相同的对象。

1)您可以将此行移到 foreach 之外:

                    cont.DailySales.InsertOnSubmit(d);// update the value
                    cont.SubmitChanges();

2) 在第一圈插入物体。在接下来的轮次中更新相同的对象。

                    cont.UpdateObject(d);
                    cont.SaveChanges();
于 2013-04-05T11:53:00.320 回答
0

你应该只插入一次

 using (transactionClassDataContext cont = new transactionClassDataContext())
            { 
                int counter = 0;
                DailySale d = new DailySale();
                cont.DailySales.InsertOnSubmit(d);// **INSERT** the value

并更新其余时间

            var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);
            var query2 = (from r in cont.Transactions where r.Date.Equals(Date) select r);
            foreach (var z in query)
            {
                counter++;

            }
            if (counter>0)
            {

                    foreach (var y in query2)
                    {

                        takings = takings + y.Price;
                        Expenses = Expenses + 0;

                        d.Expenses += 0;
                        d.Takings += y.Price;
                        d.Profit = d.Takings - d.Expenses;
                        d.Date = Date;


                        cont.SubmitChanges(); // ***  left it here but better move it outside the foreach, 

                    }              

`cont.SubmitChanges(); 甚至可以完全移动到最后,因此您只有一笔交易。

于 2013-04-07T08:19:45.893 回答
0

事实证明我正在更改 d (每日销售表)而不是更改“ var r”

            DailySale d = new DailySale();
            var query = (from q in cont.DailySales where q.Date.Equals(Date) select q);


            foreach (var z in query)
            {
                counter++;
            }


            if (counter>0)
            {
                foreach (var r in query)
                {

                    r.Takings = r.Takings + (decimal)price; //  here i was using d instead of r
                    r.Profit = r.Takings - r.Expenses;
                }
                cont.SubmitChanges(); 

谢谢您的帮助 。

于 2013-04-08T13:00:48.763 回答