After having played around with LINQ and reading the literature on it I have
not been successful in getting this to work so I am looking for some guidance:
I want to find a hunk in a collection whose Start field is the largest in
the collection but less than an arbitrary cutoff ("selectedRow"). The result
of the query should just be that single object of type Hunk. (No two objects
will have the same value for their Start fields.) Here is the code that I
thought might work:
Hunk myHunk =
(Hunk) from h in hunkList
where h.Start < selectedRow
group h by h.Start into g
select g.Max(g2 =g2.Start);
This seems to have two issues:
(1) Runtime error on the cast to Hunk so apparently I am not selecting a
single Hunk.
(2) If I store the result just in a var, the debugger shows me that there
are no objects in the result collection.
To be clear on what I am seeking, here is a SQL-equivalent version of what I
want:
select * from hunkList
where start =
(select max(start) from hunkList
where start < selectedRow)
)
Thanks!