Neat way of gettings position of my Object in linq collections
- by Steve
I currently have a object called Week. A week is part of a Season object. the season can contain many weeks. What I want to do is find the position of my week (is it the first week in the season (so #1) or is it the second (so #2).
int i = 0;
foreach ( var w in Season.Weeks.OrderBy(w => w.WeekStarts)){
if(w.Id == Id){
return i;
}
i+=1;
}
At the moment this is what I have. I order the weeks in a second by there start date to make sure they are in the correct order. and I cycle through them until I find the week that matches the week I am currently looking at. and return the int that I have been counting up..
I feel there should be a easier linq way to do this as it feels pretty messy!