私がやろうとしているのは、WCF サービスを使用して 1 つのデータベース (トランザクション) を通過するメソッドを作成し、今日の日付を持つテーブルからすべてを取得し、それらを日ごとに 1 つの行を表示する日次売上テーブルに追加することです。利益、毎日の収入、費用など。
私はそれをこのようにしようとしました
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 の新しいインスタンスを作成する必要があると述べていますが、更新された合計を含む 1 つの行だけが必要な場合、m の毎日の売上に大量のレコードを追加するだけのようです。
何か案は?