---
title: "SQL-109: Retrieve Case"
summary: Read operation against Case via NHibernate ORM.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# SQL-109: Retrieve Case

This ORM statement performs a read operation on `Case` via `IUnitOfWork.GetRepository<T>()`, executed from `Services/SAIF.Services/Collections/GenerateTasksService.cs (L92, L131, L189, L205)`.

## Classification

| Field | Value |
| --- | --- |
| **SQL ID** | SQL-109 |
| **Operation type** | Read |
| **Execution method** | ORM (NHibernate LINQ) |
| **Database** | Oracle |
| **Schema** | [unqualified]** |
| **Command type** | N/A (ORM-generated) |
| **Business object** | `Case` |
| **Criteria property** | N/A — ORM |
| **Lifecycle method** | `BalanceAtMinimumTasks, JudgmentExpiringTasks, PolicyPaymentTasks, ThirdPartyPaymentTasks` |

## Parameters

| Parameter | Direction | Type | Max length | Description |
| --- | --- | --- | --- | --- |
| `selectedPayment.CaseId` | Input | ** | N/A | `x.Id == selectedPayment.CaseId` → column LEGAL_COLLECTION_SEQ_NUM |
| `newPayment.CaseId` | Input | ** | N/A | `x.Id == newPayment.CaseId` → column LEGAL_COLLECTION_SEQ_NUM |
| `0` | Input | ** | N/A | `x.JudgmentYears.NumberOfYears > …` → column join via `JudgmentYears` (JUDGMENT_YEARS) → `JudgmentYears.NumberOfYears` |
| `0` | Input | ** | N/A | `x.CurrentBalanceAmount > …` → column CURRENT_BALANCE_AMT |
| `"C"` | Input | ** | N/A | `x.StatusCode.Code != "C"` → column join via `StatusCode` (LEGAL_COLLECTION_STATUS_CODE) → `StatusCode.Code` |

## Tables

| Table | Access | Columns used |
| --- | --- | --- |
| [LEGAL_COLLECTION](../database-tables.md#legal_collection) | SELECT | `LEGAL_COLLECTION_SEQ_NUM`, `UPDATE_NUM`, `POLICY_NUM`, `SAIF_CLAIM_NUM`, `RECEIVED_DATE`, `RECORDING_COUNTY_CODE`, `STATE`, `COURT_CASE_NUM`, `CURRENT_BALANCE_AMT`, `UPDATE_USER`, `UPDATE_DATE`, `CREDIT_REP_USER`, `INTEREST_CALC_OPTION_CODE`, `INTEREST_ANNUAL_RATE`, `JUDGMENT_CODE`, `JUDGMENT_YEARS`, `LEGAL_COLLECTION_CODE`, `LEGAL_COLLECTION_STATUS_CODE` |

## LINQ statement

Source: `Services/SAIF.Services/Collections/GenerateTasksService.cs:92`

```csharp
// Services/SAIF.Services/Collections/GenerateTasksService.cs:92-94
                selectedPayment.Case = UnitOfWork.GetRepository<DataModel.Case>()
                  .Query()
                  .SingleOrDefault(x => x.Id == selectedPayment.CaseId);

// Services/SAIF.Services/Collections/GenerateTasksService.cs:131-133
                selectedPayment.Case = UnitOfWork.GetRepository<DataModel.Case>()
                  .Query()
                  .SingleOrDefault(x => x.Id == selectedPayment.CaseId);

// Services/SAIF.Services/Collections/GenerateTasksService.cs:189-191
                newPayment.Case = UnitOfWork.GetRepository<DataModel.Case>()
                  .Query()
                  .SingleOrDefault(x => x.Id == newPayment.CaseId);

// Services/SAIF.Services/Collections/GenerateTasksService.cs:205-210
            IEnumerable<DataModel.Case> expiringCases = UnitOfWork.GetRepository<DataModel.Case>()
                .Query()
                .Where(
                    x => x.JudgmentYears.NumberOfYears > 0
                    && x.CurrentBalanceAmount > 0
                    && x.StatusCode.Code != "C");

// Services/SAIF.Services/Collections/GenerateTasksService.cs:216
            foreach (var expiringJudgment in expiringCases)
```

## 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
-- Services/SAIF.Services/Collections/GenerateTasksService.cs:92
SELECT
  LC.LEGAL_COLLECTION_SEQ_NUM,
  LC.UPDATE_NUM,
  LC.POLICY_NUM,
  LC.SAIF_CLAIM_NUM,
  LC.RECEIVED_DATE,
  LC.RECORDING_COUNTY_CODE,
  LC.STATE,
  LC.COURT_CASE_NUM,
  LC.CURRENT_BALANCE_AMT,
  LC.UPDATE_USER,
  LC.UPDATE_DATE,
  LC.CREDIT_REP_USER,
  LC.INTEREST_CALC_OPTION_CODE,
  LC.INTEREST_ANNUAL_RATE,
  LC.JUDGMENT_CODE,
  LC.JUDGMENT_YEARS,
  LC.LEGAL_COLLECTION_CODE,
  LC.LEGAL_COLLECTION_STATUS_CODE
FROM LEGAL_COLLECTION LC
WHERE LC.LEGAL_COLLECTION_SEQ_NUM = :CaseId
-- NHibernate expects at most one row

-- Services/SAIF.Services/Collections/GenerateTasksService.cs:131
SELECT
  LC.LEGAL_COLLECTION_SEQ_NUM,
  LC.UPDATE_NUM,
  LC.POLICY_NUM,
  LC.SAIF_CLAIM_NUM,
  LC.RECEIVED_DATE,
  LC.RECORDING_COUNTY_CODE,
  LC.STATE,
  LC.COURT_CASE_NUM,
  LC.CURRENT_BALANCE_AMT,
  LC.UPDATE_USER,
  LC.UPDATE_DATE,
  LC.CREDIT_REP_USER,
  LC.INTEREST_CALC_OPTION_CODE,
  LC.INTEREST_ANNUAL_RATE,
  LC.JUDGMENT_CODE,
  LC.JUDGMENT_YEARS,
  LC.LEGAL_COLLECTION_CODE,
  LC.LEGAL_COLLECTION_STATUS_CODE
FROM LEGAL_COLLECTION LC
WHERE LC.LEGAL_COLLECTION_SEQ_NUM = :CaseId
-- NHibernate expects at most one row

-- Services/SAIF.Services/Collections/GenerateTasksService.cs:189
SELECT
  LC.LEGAL_COLLECTION_SEQ_NUM,
  LC.UPDATE_NUM,
  LC.POLICY_NUM,
  LC.SAIF_CLAIM_NUM,
  LC.RECEIVED_DATE,
  LC.RECORDING_COUNTY_CODE,
  LC.STATE,
  LC.COURT_CASE_NUM,
  LC.CURRENT_BALANCE_AMT,
  LC.UPDATE_USER,
  LC.UPDATE_DATE,
  LC.CREDIT_REP_USER,
  LC.INTEREST_CALC_OPTION_CODE,
  LC.INTEREST_ANNUAL_RATE,
  LC.JUDGMENT_CODE,
  LC.JUDGMENT_YEARS,
  LC.LEGAL_COLLECTION_CODE,
  LC.LEGAL_COLLECTION_STATUS_CODE
FROM LEGAL_COLLECTION LC
WHERE LC.LEGAL_COLLECTION_SEQ_NUM = :CaseId
-- NHibernate expects at most one row

-- Services/SAIF.Services/Collections/GenerateTasksService.cs:205
SELECT
  LC.LEGAL_COLLECTION_SEQ_NUM,
  LC.UPDATE_NUM,
  LC.POLICY_NUM,
  LC.SAIF_CLAIM_NUM,
  LC.RECEIVED_DATE,
  LC.RECORDING_COUNTY_CODE,
  LC.STATE,
  LC.COURT_CASE_NUM,
  LC.CURRENT_BALANCE_AMT,
  LC.UPDATE_USER,
  LC.UPDATE_DATE,
  LC.CREDIT_REP_USER,
  LC.INTEREST_CALC_OPTION_CODE,
  LC.INTEREST_ANNUAL_RATE,
  LC.JUDGMENT_CODE,
  LC.JUDGMENT_YEARS,
  LC.LEGAL_COLLECTION_CODE,
  LC.LEGAL_COLLECTION_STATUS_CODE
FROM LEGAL_COLLECTION LC
WHERE LC.JUDGMENT_YEARS > 0
  AND LC.CURRENT_BALANCE_AMT > 0
  AND LC.LEGAL_COLLECTION_STATUS_CODE <> 'C'
```

Translation notes:
- `selectedPayment.CaseId` / `newPayment.CaseId` -> bind `:CaseId`.
- L205-210 is enumerated by `foreach` (L216). The `ReceivedDate.AddYears(...)` date-window filter (L218-220) runs in memory (the source comment at L211-213 records that it was unsupported in LINQ).
- `Case` has `HasMany` Activities/Debtors/Transactions with cascade (CaseMap.cs); collections load lazily with their own statements and are not part of this SELECT. `References` (`CREDIT_REP_USER`, `INTEREST_CALC_OPTION_CODE`, `INTEREST_ANNUAL_RATE`, `JUDGMENT_CODE`, `JUDGMENT_YEARS`, `LEGAL_COLLECTION_CODE`, `LEGAL_COLLECTION_STATUS_CODE`) contribute FK columns only; none of the referenced tables is joined.

## Operations and processes
- GenerateTasksService.BalanceAtMinimumTasks — see [Operations and processes](../operations-processes.md)
- GenerateTasksService.JudgmentExpiringTasks — see [Operations and processes](../operations-processes.md)
- GenerateTasksService.PolicyPaymentTasks — see [Operations and processes](../operations-processes.md)
- GenerateTasksService.ThirdPartyPaymentTasks — see [Operations and processes](../operations-processes.md)

## Sources
- `Services/SAIF.Services/Collections/GenerateTasksService.cs (L92, L131, L189, L205)`
