private void GetOperationRooms(List <ProcedureEntity> list, ref Procedure procedure) { // get the list of all the operation. List <Operation> operationList = GetOperationsList(list, procedure); // get the unique list of operation room. var opRoomsName = operationList.Select(o => o.ORName).Distinct(); // create operation room object with the each foreach (string opRoomName in opRoomsName) { OperationRoom operationRoom = new OperationRoom(); operationRoom.Name = opRoomName; // get all the operations from from opRoomName var opList = from op in operationList where op.ORName == opRoomName select op; // add the list of operation to the operation room. operationRoom.Add(opList); // add the operation room to the procedure object. procedure.AddOperationRoom(operationRoom); } }