2017-07-10 85 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) 
    } 
相关问题