Home > Net >  C # the datatable group by using Linq sum when the field value is NULL will go wrong
C # the datatable group by using Linq sum when the field value is NULL will go wrong

Time:11-08

 
//-- -- -- -- -- -- -- -- -- the whole batch of accounting subject summary table
Var result=the from r in DsOutOrder. Tables. [1] AsEnumerable ()//DtAllShd AsEnumerable ()
Where r [r]. "TempCol_bj" ToString ()!="Del"
Group r by r.F ield (" HSZT ") into g
Select new
{
G_hsztjc=g. ey,
G_ccxs=g.S um (r=& gt; R.F ield (" CCXS ")),//CCXS field values are NULL
//g_ccxs=g.S um (r=& gt; R.F ield (" CCXS ")),
G_tjmd=g.S um (r=& gt; R.F ield (" TJMD ")),
G_fymd=g.S um (r=& gt; R.F ield (" fymd ")),//fymd field values are NULL
G_sdmd=g.S um (r=& gt; R.F ield (" SDMD "))
};
DgvTotalHszt. Rows. The Clear ();
Double [] zSum=new Double [] {0, 0, 0, 0};
Foreach (var re result) in//program here does not perform the
{
Int rowIndex=DgvTotalHszt. Rows. The Add ();
DgvTotalHszt. Rows [rowIndex] Cells [0]. Value=https://bbs.csdn.net/topics/re.g_hsztjc;
DgvTotalHszt. Rows [rowIndex] Cells [1]. The Value=https://bbs.csdn.net/topics/re.g_ccxs;
DgvTotalHszt. Rows [rowIndex] Cells [2]. The Value=https://bbs.csdn.net/topics/re.g_tjmd;
DgvTotalHszt. Rows [rowIndex] Cells [3]. The Value=https://bbs.csdn.net/topics/re.g_fymd;
DgvTotalHszt. Rows [rowIndex] Cells [4]. The Value=https://bbs.csdn.net/topics/re.g_sdmd;
ZSum [0] +=re. G_ccxs;
ZSum [1] +=re. G_tjmd;
ZSum [2] +=re. G_fymd;
ZSum [3] +=re. G_sdmd;

}
Int rowIndex2=DgvTotalHszt. Rows. The Add ();
DgvTotalHszt. Rows [rowIndex2] Cells [0]. Value="https://bbs.csdn.net/topics/or meter";
DgvTotalHszt. Rows [rowIndex2] Cells [1]. The Value=https://bbs.csdn.net/topics/zSum [0];
DgvTotalHszt. Rows [rowIndex2] Cells [2]. The Value=https://bbs.csdn.net/topics/zSum [1];
DgvTotalHszt. Rows [rowIndex2] Cells [3]. The Value=https://bbs.csdn.net/topics/zSum [2];
DgvTotalHszt. Rows [rowIndex2] Cells [4]. The Value=https://bbs.csdn.net/topics/zSum [3].
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Field CCXS, TJMD fymd, SDMD four fields have value is normal, NULL when abnormal, run to the foreach when they didn't,

CodePudding user response:

Try
G_fymd=g.S um (r=& gt; R.F ield (" fymd ")?? 0)

CodePudding user response:

The positive solution, upstairs

CodePudding user response:

Benefit, bloggers to share, thank you

CodePudding user response:

CSDN now how so less people answer ah, just can't wait for answers, yesterday oneself can all that trouble, here for your reference: check if r is null if it is null returns 0, people outside, go back to the original, please fill in,

CodePudding user response:

Positive solution # 1,
Actually is the problem caused by Double + null,
So let's convert null to 0
  •  Tags:  
  • C#
  • Related