Background:
I'm designing a list-like control (WinForms) that's backed by a DbSet
. A chief requirement is that it doesn't load the entire list into local memory. I'm using a DataGridView
in virtual mode as the underlying UI. I'm planning to implement the CellValueNeeded
function as orderedQueryable.ElementAt(n)
.
Problem:
I need to allow the control's consumer to get/set the currently-selected value, by value rather than by index. Getting is easy--it's the same as the CellValueNeeded operation--but setting is harder: it requires me to get the index of a given element. There's not a built-in orderedQueryable.FirstIndexOf(value)
operation, and although I could theoretically fake it with some sort of orderedQueryable.SkipWhile
shenanigans where the expression has a side-effect, in practice the DbSet's query provider probably doesn't support doing that.
Questions:
Is there an efficient way to get the index of a particular value within an IOrderedQueryable
? How?
(If this approach turns out to be untenable, I'd settle for suggestions on how I might restructure the problem to make it solvable.)
Side notes: Elements can be inserted and removed from the list, in which case the old indices will be invalid--that's acceptable, since they're never exposed to the consumer. It's an error for the consumer to attempt to select an item that isn't actually in the list, and actually the consumer would have gotten the item from the list in the first place (although perhaps the indices have changed since then).