---
title: "SQL-222: Retrieve LegalBillService by primary key"
summary: Reload a LegalBillService by its identifier before applying adjustments, executed once per service line in a loop.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# SQL-222: Retrieve LegalBillService by primary key

`BillSaveService.PrepareAdjustedBillForSave` and `SaveBillServices` each reload a `LegalBillService` by its primary key before falling back to a new instance (`?? new BillData.LegalBillService()`, evaluated client-side). Both call sites run inside a `foreach` over the bill's service lines — an N+1 pattern.

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-222 |
| **Operation type** | Read |
| **Execution method** | ORM (NHibernate LINQ) |
| **Database** | Oracle |
| **Schema** | [unqualified]** |
| **Command type** | N/A (ORM-generated) |
| **Business object** | `LegalBillService` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `PrepareAdjustedBillForSave, SaveBillServices` |

## Parameters

| Parameter | Direction | Type | Max length | Description |
| --- | --- | --- | --- | --- |
| `:serviceId` | Input | long | N/A | `service.Id` (statement 1) / `legalBillService.Id` (statement 2) |

## Tables

| Table | Access | Columns used |
| --- | --- | --- |
| [LEGAL_BILL_SERVICE](../database-tables.md#legal_bill_service) | SELECT | all 18 mapped columns; filter LEGAL_BILL_SERVICE_SEQ_NUM |

## LINQ statement

Source: `Services/SAIF.Services/Legal/Bills/BillSaveService.cs:186`, also `:423`.

```csharp
// Services/SAIF.Services/Legal/Bills/BillSaveService.cs:186
_unitOfWork.GetRepository<BillData.LegalBillService>()
    .Query()
    .FirstOrDefault(x => x.Id == service.Id) ?? new BillData.LegalBillService();

// Services/SAIF.Services/Legal/Bills/BillSaveService.cs:423
_unitOfWork.GetRepository<BillData.LegalBillService>()
    .Query()
    .FirstOrDefault(x => x.Id == legalBillService.Id) ?? new BillData.LegalBillService();
```

## Sample SQL

> **Inferred SQL:** This SQL was inferred from the LINQ statement above and the NHibernate mappings. It is not concrete SQL from source code; NHibernate generates the executed statement at runtime, so aliases, column order, join syntax, and bind-parameter names may differ.

```sql
-- Inferred from LINQ (NHibernate mapping translation); not concrete SQL
-- (1) L186 PrepareAdjustedBillForSave, (2) L423 SaveBillServices
SELECT * FROM (
  SELECT S.LEGAL_BILL_SERVICE_SEQ_NUM,
       S.UPDATE_NUM,
       S.AMENDED_FLAG,
       S.SERVICE_FROM_DATE,
       S.SERVICE_THRU_DATE,
       S.BILLED_AMOUNT,
       S.PAID_AMOUNT,
       S.BILLED_UNITS,
       S.ADJUSTMENT_REASON_TEXT,
       S.PARENT_BILL_SERVICE_SEQ_NUM,
       S.CREATE_USER,
       S.CREATE_DATE,
       S.UPDATE_USER,
       S.UPDATE_DATE,
       S.LEGAL_BILL_SEQ_NUM,
       S.BILL_SERVICE_CODE,
       S.BILL_SERVICE_RATE_TYPE_CODE,
       S.ADJUSTMENT_REASON_CODE
  FROM   LEGAL_BILL_SERVICE S
  WHERE  S.LEGAL_BILL_SERVICE_SEQ_NUM = :serviceId    -- (1) service.Id; (2) legalBillService.Id
) WHERE ROWNUM <= 1
```

Translation notes:
- `FirstOrDefault` → Oracle10 dialect wraps the query in `SELECT * FROM (...) WHERE ROWNUM <= 1`.
- Executed once per service line inside `foreach` loops (L183, L419), which is an N+1 pattern. `?? new BillData.LegalBillService()` is client-side.

## Operations and processes
- BillSaveService.PrepareAdjustedBillForSave — see [Operations and processes](../operations-processes.md)
- BillSaveService.SaveBillServices — see [Operations and processes](../operations-processes.md)

## Sources
- `Services/SAIF.Services/Legal/Bills/BillSaveService.cs (L186, L423)`
