使用下面提到的查询,我得到了名为 促销值(value) 。我想得到 *matched *
的所有值的总和var matched = from table1 in dvtempPropertyRoomRatePromotion.ToTable().AsEnumerable()
join table2 in dvPropertyRooms.ToTable().AsEnumerable() on
table1.Field<DateTime>("RateDate") equals table2.Field<DateTime>("RateDate")
where table1.Field<DateTime>("RateDate") == table2.Field<DateTime>("RateDate")
select table1.Field<string>("promotionValue");
最佳答案
您需要parse int
或 decimal
的字符串:
var matched = from r1 in dvtempPropertyRoomRatePromotion.ToTable().AsEnumerable()
join r2 in dvPropertyRooms.ToTable().AsEnumerable()
on r1.Field<DateTime>("RateDate").Date equals r2.Field<DateTime>("RateDate").Date
select decimal.Parse(r1.Field<string>("promotionValue"));
decimal sum = matched.Sum();
请注意,我还更改了一些其他内容,例如多余的 where
(因为您已经加入了这些表)或 DateTime 的
。Date
属性
除此之外
- 您为什么需要
DataView
?ToTable
始终创建一个新的DataTable
。为什么不对所有人使用Linq-To-DataSet
?我假设您已使用DataView
进行过滤,请改用Enumerable.Where
。这将更一致、更高效且更具可读性。 - 为什么
promotionValue
列是一个字符串?您应该将其存储为数字类型。
关于c# - 使用 LINQ 计算特定列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499976/