# Distinct

> Removes duplicate rows from a query result. On whole entity rows it is a no-op, because rows are already unique by Id — it earns its keep on projections, where duplicates are real.

<!-- id: query-distinct · area: query · stability: stable · html: https://osysharp.com/reference/query/distinct/ -->

## Summary        {#summary}
`.Distinct()` removes duplicates from a result. It takes no arguments — "distinct" means *the whole row*, exactly as
in SQL.

## Signature      {#signature}
```osy syntax
<query>.Distinct().ToList()
```

## Description    {#description}

### On whole rows it does nothing   {#no-op-on-rows}
Entity rows are already unique — each has its own `Id` — so `Order.Where(…).Distinct()` cannot remove anything. It is
harmless, and it is also pointless, and writing it usually means someone expected it to do something it does not:

```osy title="distinct over entity rows changes nothing" test app=query-distinct
entity Order {
  [Required] string Code;
  [MaxLength(100)] string Region;
  decimal Total;
}

Order[] All() {
  return Order.Where(o => o.Total > 0).Distinct().ToList();   // same rows, either way
}
```

If what you meant was "one order per region", that is not `Distinct` — it is a grouping, or a projection of the region
alone.

### When does `Distinct` actually remove something?   {#projections}
Duplicates are real the moment you stop selecting whole rows. Two orders from the same region project to the same
region string — and *that* is where `Distinct` does the work you wanted:

```osy title="the regions we have orders in" test app=query-distinct
int RegionCount() {
  return Order.Where(o => o.Total > 0).Distinct().ToList().Count;
}
```

### What does `Distinct` cost?   {#cost}
De-duplicating means the database must compare rows, which usually means sorting them. On a large result that is real
work. If you are reaching for `Distinct` to paper over a join that is producing duplicates, fix the join — the
duplicates are a symptom, and `Distinct` only hides it.

## See also       {#see-also}
- [Where / Single / Count](https://osysharp.com/reference/query/where/) — the query being de-duplicated
- [ToList](https://osysharp.com/reference/query/tolist/) — materialising the result
- [Union / Concat / Intersect / Except](https://osysharp.com/reference/query/set-operators/) — union / intersect / except, which also concern duplicates
