2

我正在运行以下查询:-

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
            group data by new
            {
                InvNo = data.Field<double>("InvoiceNo"),
                AccRef = data.Field<double>("SiteRefNum"),
            }
            into g
            select new
            {
                Code = "1",
                InvType = "I",
                Account = g.Key.AccRef,
                InvNo = g.Key.InvNo,
                ChargeTotal = g.Sum(d => d.field<double>("Charge")
            };

由于数据导入数据表(从 Excel)的方式,有时 AccRef 的数据类型是double,有时是string. 有没有办法在运行时克服这个问题,因为我不希望用户在导入之前修改 Excel 中的源数据。

4

1 回答 1

3

您可以Convert.ToDouble与对象一起使用,因此它应该适用于双精度和字符串:

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
             group data by new
             {
                 InvNo = data.Field<double>("InvoiceNo"),
                 AccRef = Convert.ToDouble(data["SiteRefNum"]),
             }
             into g
             select new
             {
                 Code = "1",
                 InvType = "I",
                 Account = g.Key.AccRef,
                 InvNo = g.Key.InvNo,
                 ChargeTotal = g.Sum(d => d.Field<double>("Charge"))
             };

当然,只有在SiteRefNum实际可转换为double.

于 2013-02-04T15:07:45.847 回答