adding list's elements respect to a field of the elements in Scala

31 Views Asked by At

In Scala I have the next command:

  lPServ <- Pservs.getAll(.....some logical condition.....)

each element of lPserv has a "price" and "quantity" fields. For this list, I need to get the total of adding each

         e.price * e.quantity

where e is element of lPServ. Any ideas?

Thx

1

There are 1 best solutions below

0
On BEST ANSWER

If Pservs is:

case class Pservs(price: Int, quantity: Double)

then:

( for { e <- Seq(Pservs(12, 3), Pservs(6, 3)) } yield e.price * e.quantity ).sum