Thursday, 15 August 2013

Linq to object - ranking items out of 100 in descending order -



Linq to object - ranking items out of 100 in descending order -

i have next array of numbers:

dim lst() integer = {4, 3, 200, 250, 670, 1, 450, 3, 10, 15, 900, 450}

i need list them series of objects number , corresponding rank out of 100, results below:

number rank 900 100 670 90 450 80 450 80 250 60 200 50 15 40 10 30 4 20 3 10 3 10 1 0

i'm stumped - i've got this:

dim t = l in lst order l descending select new { .number = l, .rank = ((from o in lst o > l select o).distinct.count + 1)}

this technique used on series of lists of approximately 3000+ objects, , suspect performance of approach dire , improved when utilize total dataset.

any suggestions appreciated...

fixed it... pretty close already:

dim lst() integer = {4, 3, 200, 250, 670, 1, 450, 15, 15, 15, 900, 450} dim t = (from l in lst order l descending).select( function(l, index) new { .number = l, .rank = 100 - (((from o in lst o > l select o).count + 1) - 1) * 100 / lst.count})

linq

No comments:

Post a Comment