min - Finding minimum time from record and splitting it in LINQ -
we have database of swimmers times. create ranking want fastest time of each athlete.
var rankings = ( r in _db.results orderby r.swimtime group r r.athleteid rg select new { athleteid = rg.key, firstname = rg.min(f2 => f2.athlete.firstname), swimtime = rg.min(f8 => f8.swimtime), hours = rg.min(f9 => f9.swimtime.hours), minutes = rg.min(f10 => ("00" + f10.swimtime.minutes.tostring()).substring(("00" + f10.swimtime.minutes.tostring()).length - 2)), // 2 digits in minutes seconds = rg.min(f11 => ("00" + f11.swimtime.seconds.tostring()).substring(("00" + f11.swimtime.seconds.tostring()).length - 2)), // 2 digits in seconds milliseconds = rg.min(f12 => (f12.swimtime.milliseconds.tostring() + "00").substring(0, 2)), // because miliseconds not filled } );
now ranking created correctly, time shown not.
know problem is, don't know how fix it:
in database have swimmer has 2 times : 00:01:02:10 (1min2sec10) , 00:00:56:95 (56sec95)
the result minimum minutes (=00), minimum seconds (=02) , minimum milliseconds (=10)
resulting in time of 00:00:02:10.
what should hours,minutes,seconds , milliseconds of fastest time (=00:00:56:95)
anyone ideas on how fix ?
this should trick:
from result in db.results group result result.athleteid g let bestresult = ( athleteresult in g orderby athleteresult.swimtime select athleteresult).first() orderby bestresult.swimtime select new { athleteid = bestresult.athlete.id, firstname = bestresult.athlete.firstname, besttime = bestresult.swimtime, }
the query fetches best result group (all results single athelete), orders result, , uses result populate final result.
Comments
Post a Comment