Neat way of gettings position of my Object in linq collections

Posted by Steve on Stack Overflow See other posts from Stack Overflow or by Steve
Published on 2011-02-17T23:12:14Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 179

Filed under:
|

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ