試試這個代碼:
var afters = ctx.headers.SkipWhile(((x, index) => index <= currentItemIndex))
.TakeWhile((x, index) => index != ctx.headers.Count()-1).ToList();
for (int i = currentItemIndex+1, j=0; j < afters.Count; i++ ,j++)
{
var befores = ctx.headers.TakeWhile((x, index) => index <i);
afters[j].BeforeSSAmount = befores.SelectMany(x => x.StationStateItem).Sum(x => x.Amount);
}
編輯使用的ListCollectionView在WPF:
var afters = header.DataSource.Cast<StationStateHdr>().SkipWhile(((x, index) => index <= header.DataSource.CurrentPosition))
.TakeWhile((x, index) => index != header.DataSource.Cast<StationStateHdr>().Count()-1).ToList();
for (int i = header.DataSource.CurrentPosition+1, j=0; j < afters.Count; i++ ,j++)
{
var befores = header.DataSource.Cast<StationStateHdr>().TakeWhile((x, index) => index <i);
afters[j].BeforeSSAmount = befores.SelectMany(x => x.StationStateItem).Sum(x => x.Amount);
}
或此代碼:
var listOfStationStateHdr = header.DataSource.Cast<StationStateHdr>();
listOfStationStateHdr.SkipWhile(((x, index) => index <= header.DataSource.CurrentPosition))
.TakeWhile((x, index) => index != header.DataSource.Cast<StationStateHdr>().Count() - 1).ToList().ForEach(x =>
{
x.BeforeSSAmount = listOfStationStateHdr.TakeWhile(y => y.Id < x.Id).SelectMany(b => b.StationStateItem).Sum(s => s.Amount);
});
或
var sumCurrent = CurrentStationStateHdr.StationStateItem.Sum(t => t.Amount);
var listOfStationStateHdr = header.DataSource.Cast<StationStateHdr>().ToList();
double? nextItembeforeSSAmount = listOfStationStateHdr[header.DataSource.CurrentPosition + 1].BeforeSSAmount;
listOfStationStateHdr.SkipWhile(((x, index) => index <= header.DataSource.CurrentPosition))
.TakeWhile((x, index) => index != header.DataSource.Cast<StationStateHdr>().Count() - 1).ToList().ForEach(x =>
{
x.BeforeSSAmount += sumCurrent - nextItembeforeSSAmount;
});
你應該先嚐試......你總是問的問題和回答沒有。 –
你想要更新兩個表。手動做到這一點。或者atleast向我們顯示您的對象類爲這些..也顯示更新的功能Table StationStateItem –
我想更新StationStateHdr表中的BeforeSSAmount,當我更改StationStateItem表中的Amout並單擊保存按鈕時。 –