2017-07-10 113 views
0

我有這個StationStateItem表這樣的數據:如何根據EntityFramework中的另一個表更新表?

Id SSHdrRef  Amount 
--------------------------- 
1 1(forrein Key) 1 
2 1    1 
3 2    2 
4 2    2 
5 3    1 

我想,當金額StationStateItem改爲更新StationStateHdr表。

BeforeSSAmount=StationStateHdr.StationStateItem.Sum(x=> x.Amount) 

StationStateHdr表是這樣的:

Id BeforeSSAmount 
-------------- 
1 0 
2 2 (1+1) 
3 6 (1+1+2+2) 
+2

你應該先嚐試......你總是問的問題和回答沒有。 –

+1

你想要更新兩個表。手動做到這一點。或者atleast向我們顯示您的對象類爲這些..也顯示更新的功能Table StationStateItem –

+0

我想更新StationStateHdr表中的BeforeSSAmount,當我更改StationStateItem表中的Amout並單擊保存按鈕時。 –

回答

1

試試這個代碼:

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; 
     }); 
+0

這是什麼?她甚至沒有完整的問題?? –

+0

感謝您的幫助,但什麼是currentItemIndex? –

+0

我有自定義的WPF DataGridNavigator控件,它具有ListCollectionView的DataSource屬性。 –

2

try代碼

foreach(var a in StationStateHdr) 
    { 
    a .BeforeSSAmount=StationStateHdr.StationStateItem.where(c=>c.SSHdrRef<a.id).Sum(x=> x.Amount) 
    } 
相關問題