In Scala, how do I return a list of all object instances with a property set to a certain value
<p>I am using Scala, and would like to be able to select all instances of an object with a property set to a given value. Say I have an Order class and a Order Item class, and I want to select or pull all of the order items with an order id of say 1, and return all of these in a list, how do I do this?</p>

<p>I would like to be able to return a list of order line items that have an order id of say 1</p>

<p>Here is my Line Item class definition:
Code: </p>

<pre><code>case class LineItem(val itemId : Int, val orderId:Int, val productId:Int, val quantity:Int)
</code></pre>

<p>I then have some order items defined in an object:</p>

<p>Code:</p>

<pre><code>object LineItem {

  val li1 = new LineItem(1, 1, 1, 10)
  val li2 = new LineItem(2, 1, 4, 1)
  val li3 = new LineItem(3, 1, 2, 1)
  val li4 = new LineItem(4, 1, 3, 1)
  val li5 = new LineItem(5, 2, 1, 1)
  val li6 = new LineItem(6, 1, 7, 1)

  var lineItems = Set(li1, li2, li3, li4, li5, li6)

  def findItemsInOrder(ordId:Int) = lineItems.find(_.orderId == ordId)

  }
</code></pre>

<p>As you can see 5 of the line items belong to the order with an id of 1.
So first a list of orders are printed out for the user to see, then I want the user to be able to select an order to see all the line items within that order, so I would like these line items to be printed out in the shell.</p>

<p>So the order id is inputted by the user:
Code:</p>

<pre><code>val orderNo = readLine("Which order would you like to view?").toInt
</code></pre>

<p>And then this line:
Code:</p>

<pre><code>println("OrderID: " + LineItem.findItemsInOrder(orderNo).get.orderId + ", ProductID: " + LineItem.findItemsInOrder(orderNo).get.productId + ", Quantity: " + LineItem.findItemsInOrder(orderNo).get.quantity)
</code></pre>

<p>Only prints out the first line item with an order id of 1. </p>

<p>I have then tried to loop through all line items, like this:</p>

<p>Code:</p>

<pre><code>var currentLineItems = new ListBuffer[LineItem]()

for (item &lt;- LineItem.lineItems){
  val item1: LineItem = LineItem(LineItem.findItemsInOrder(orderNo).get.lineId, LineItem.findItemsInOrder(orderNo).get.orderId, LineItem.findItemsInOrder(orderNo).get.productId, LineItem.findItemsInOrder(orderNo).get.quantity)
  currentLineItems += item1
}

for (item &lt;- currentLineItems ){
  println("OrderID: " + LineItem.findItemsInOrder(orderNo).get.orderId + ", ProductID: " + LineItem.findItemsInOrder(orderNo).get.productId + ", Quantity: " + LineItem.findItemsInOrder(orderNo).get.quantity)
}
</code></pre>

<p>But this code just prints out the same line item 6 times.</p>

<p>I would be very grateful for any help received to help solve my problem</p>

<p>Thanks
Jackie</p>