Saturday, 15 June 2013

linq - Percentage of a row in ordered query -



linq - Percentage of a row in ordered query -

curious problem, not sure how in linq. part of problem might i'm trying @ once.

i have phase table

[id, name, [order]] {{1, 'phase 2', 2}, {2, 'phase 1', 1}

i'd percentage of progress through phases given phaseid.

i know of way sql.

select (select a.row [phase] p inner bring together (select row_number() over( order [order]) row, id [phase]) p2 on p2.id = p.id p.id = @phaseid) / convert(float, count(*)) [phase]

is there way in linq? attempts seem fail

try following:

var query = phases.orderby(x => x.order).select(x => x.id).tolist(); var result = query.select((x, i) => new { x.id, index = + 1 }) .single(x => x.id == phaseid).index / (double)query.count;

it first queries ids of phases database, correctly ordered. in memory, generates row number , filters out phase requested id. based on index , total count of phases, percentage calculated.

linq

No comments:

Post a Comment