Got a situation here, though I have a few ways to solve the problem but I’m very much feeling that it’s not the best way to solve as I’m doing it now. Let me try to explain my problem first. From Database I get an IEnumerable of NavReturnModelSP where NavReturnModelSP is a simple model class like
public class NavReturnModelSP
{
public string Scheme_Name { get; set; }
public DateTime Date { get; set; }
public double ReInvest_Nav { get; set; }
}
Now I need to first order the set by Scheme_Name & Date & then need to rebase each Scheme & Date pair value to the base of 100 for proper comparison between schemes from the very first date. I CAN’T CHANGE THE DATABASE OUTPUT. Below is a picture for your referance, Hope it’ll explain you the problem better.

And I’m solving it now just by looping through….
where ‘data’ is the IEnumerable of NavReturnModelSP
var preModel = data.OrderBy(x => x.Scheme_Name).ThenBy(x => x.Date).ToList();
var preScheme = "";
var firstNav = 0d;
for (int i = 0; i < preModel.Count(); i++)
{
if (preModel[i].Scheme_Name != preScheme)
{
firstNav = preModel[i].ReInvest_Nav;
preModel[i].ReInvest_Nav = 100;
}
else
{
if (preModel[i - 1].ReInvest_Nav != 0) // Just a check, nothing special.
preModel[i].ReInvest_Nav = 100 * preModel[i].ReInvest_Nav / firstNav;
}
preScheme = preModel[i].Scheme_Name;
}
Now I have ‘preModel’ with the rebased data in the ReInvest_Nav column. But I feel that I’m not using the proper power of the CPU & I can use Parallal programming / PLINQ to minimize the time frame as for 100000 rows for loop will be quite slow. Can someone please guide me for that.
Thanks & Happy New Year 🙂
Sanjay
Try this:
I can’t promise this will be any faster, but it avoids explicit loops.