Summary#
Inside a query predicate, list.Contains(e.Column) tests each row's column for membership in a runtime
List<T> — exactly the C#/LINQ spelling for SQL IN. It lowers to e.Column = ANY(@p), binding the whole
list as one array parameter (stable SQL shape). The list is a local you build at run time; an empty
list matches nothing.
Signature#
Entity.Where(e => <list>.Contains(e.<Column>)) // → e.Column = ANY(@p)Description#
The receiver <list> is any local collection (a new List<T>() you populate, a Text.Split result, …)
whose element type is comparable to the column — same rule as a literal IN or ==: exact for
string/bool/DateTime, id-coercion between Guid and string, and numeric widening
(int→long→decimal→double). An incompatible pair is a compile error.
This is position-sensitive: the same list.Contains(x) written outside a query predicate is the
ordinary in-memory list-membership check. It becomes a SQL IN only inside a Where/Any/Count/…
predicate, where x is a row column.
A literal list works too and is equivalent: [a, b, c].Contains(e.Column) (rendered as IN (…)); the
runtime-list form is the one that lets the set be computed at run time.
Examples#
entity Ticket { string Status; }
List<Ticket> ByIds(List<Guid> ids) {
return Ticket.Where(t => ids.Contains(t.Id)).ToList(); // t.Id = ANY(@p)
}
List<Ticket> ByStatuses() {
var open = new List<string>();
open.Add("New");
open.Add("InProgress");
return Ticket.Where(t => open.Contains(t.Status)).ToList(); // empty `open` → no rows
}See also#
- Union / Concat / Intersect / Except — combining whole row-sets (Union/Intersect/Except)
- List OrderBy (in-memory) — ranking a local list in memory