添加列表元素c#

时间:2013-10-02 06:05:33

标签: c# datareader type-conversion

伙计我有一个返回3个值的查询(-4.00,-7.53,-15.00。 我将这些名为ReaderResult的列表返回到代码中,但是当它们被添加到列表中时,我还想保留列表中添加的元素总数

 DataReader = DoQuery(" select oa.[OtherAmount]  FROM [Biatss_PC_SRV].[Pax].[SalesDocumentOtherAmount] as oa "
            +"INNER JOIN [Biatss_PC_SRV].[Pax].[SalesRelatedDocumentInformation] as rd on oa.RelatedDocumentGuid = rd.RelatedDocumentGuid "
            +"INNER JOIN  [Biatss_PC_SRV].[Pax].[SalesDocumentHeader] as h on rd.HdrGuid = h.HdrGuid where h.DocumentNumber = '" + txtTicketNumber.Text.ToString() + "'");
                //check DataReader
                if (DataReader == null)
                {
                    DataReader.Close();
                    DataReader.Dispose();
                }
                else
                {
                    if (DataReader.HasRows)
                    {
                        //setting variables to be used to initial values
                        ii = 0;
                        double sumOfTaxes = 0;

                        ReaderResult.Clear();
                        while (DataReader.Read())
                        { 
                            //for ( ii = 0; ii < DataReader.FieldCount; ii++)
                            //{
                            if (DataReader.IsDBNull(0))
                                {
                                    string CaseNull = "";
                                    ReaderResult.Add(CaseNull);
                                }
                                else
                                {
                                    //put results in LIST<>
                                    ReaderResult.Add(Convert.ToString(DataReader.GetDecimal(0)));
                                    //double sumLaSa =+ Convert.ToDouble(ReaderResult.Last());       VALUE HERE IS ONLY THE LAST OF THE 3 NOT THE SUM
                                    //sumOfTaxes =+ Convert.ToDouble(DataReader.GetDecimal(0));
                                    //double sumLaSa =+ Convert.ToDouble(ReaderResult.Last().Last()); ERROR I GET HERE says cannont convert char to double
                                    myTextboxListForAmounts[ii].Text = Convert.ToString(DataReader.GetDecimal(0));

                           // }

                            if (ii > 6)
                            { 
                                //make txtXT visible and display summed up amount in it
                                lblXT.Visible = true;
                                txtXT.Visible = true;
                                txtXT.Text = Convert.ToString(sumOfTaxes);
                            }
                            ii++;

                                }  
                        } 
                    }
                    else
                    {
                        DataReader.Close();
                        DataReader.Dispose();
                    }
                    DataReader.Close();
                    DataReader.Dispose();
                }

如何获得返回值的总和?

1 个答案:

答案 0 :(得分:0)

解决了这个问题。

使用Corak点燃的解决方案达到了目标。

 sumOfTaxes =+ Convert.ToDouble(DataReader.GetDecimal(0));

修改为

 sumOfTaxes += Convert.ToDouble(DataReader.GetDecimal(0));  (+= instead of =+)
enter code here